> I can't shove it back into CVS at the moment due to being stuck behind
> a firewall, but here's the change to Template::Stash
>
> --- perl/modules/Template/Template-Toolkit-2.06d/lib/Template/Stash.pm
> Tue Jan 22 18:09:32 2002
> +++ tt2/lib/Template/Stash.pm Fri Jan 25 14:56:52 2002
> @@ -75,6 +75,12 @@
> # eval "\$str =~ s$search$replaceg";
> return $str;
> },
> + 'match' => sub {
> + my ($str, $search) = @_;
> + return $str unless defined $str and defined $search;
> + my @matches = ($str =~ /$search/);
> + return @matches ? \@matches : '';
> + },
> 'split' => sub {
> my ($str, $split, @args) = @_;
> $str = '' unless defined $str;
>
>
> Note that it return an empty value rather than an empty list if the match
> fails. This allows you to do things like:
>
> [% IF string.match(...) %]
>
> or
>
> [% IF (matches = string.match(...)) %]
Nice! But match is almost identical to search. There is a slight
difference in the way the results are returned:
match> my @matches = ($str =~ /$search/);
match> return @matches ? \@matches : '';
search> return $str =~ /$pattern/;
Since both are called in array context, the only difference is if
there is a single capture (paren-pair): with match you end up with
a single element array, while with search you send up with a scalar
(Stash turns the array from search() into an array ref if it has at
least 2 elemets). In other cases (multiple captures or none) they
end up with the same values.
So how about we replace "search" with the new "match" and not have
two different functions?
The only change in the old search behavior would be if someone already
relies on it returning a single capture as a scalar rather than array.
Frankly that's something I could do without, since it doesn't make much
sense in certain cases. For example, with x = "0", x.search('\d') returns
true while x.search('(\d)') returns false. The new match fixes that
problem.
(Also, the new search could take an optional option argument for imsx,
in a similar way to my replace proposal.)
Craig