[Templates] "in array" function?
Mark Mills
extremely at hostile.org
Thu Mar 6 14:59:38 GMT 2008
Slaven Rezic wrote:
> Dave Howorth wrote:
>
>> Sean McAfee wrote:
>>
>>
>>> $Template::Stash::SCALAR_OPS->{isOneOf} = sub {
>>> my $str = shift;
>>> $str eq $_ and return 1 for @_;
>>> return 0;
>>> };
>>>
>>>
>> Or for added pzazz, something like:
>>
>> use Perl6::Junction qw/ any /;
>>
>> $Template::Stash::SCALAR_OPS->{isOneOf} = sub {
>> my $str = shift;
>> return (any(@_) eq $str);
>> }
>>
>> TMTOWTDI :)
>>
> Or the core module Scalar::Util (untested):
>
> use List::Util qw(first);
>
> $Template::Stash::SCALAR_OPS->{isOneOf} = sub {
> my $str = shift;
> return (first { $_ eq $str } @_;
> }
>
"first" returns the object being searched for and not a true/false
value. Searching for 0 might be a problem there...
--mark
More information about the templates
mailing list