> What I was going to say is that though the new version of Stash deals with
> most of these issues, it's still checking for trueness (rather than
> defined-ness) of the search pattern when replacing and not doing the
> replace if this isn't true.  Which means I can't do this:
> 
> [% bob = "0" %]
> bob: [% bob.replace('0','') %]   # returns '0' not ''
> 
> Or for that matter
> 
> [% wibble = "hi" %]
> wibble: [% wibble.replace('',"So: ") %]  # returns 'hi' not 'So: hi'

Yes, that's right.  I did review your earlier post before sending my
list, and I convinced myself that the issue (empty replace string) was
resolved in 2.01+, so I didn't include it.

But you are correct: a search string argument to replace() or search()
of '' or '0' doesn't work because it checks for truth (rather than
defined).

Here's an updated patch against 2.03a.

Craig

--- Template/Stash.pm.orig      Mon Jun 18 07:10:58 2001
+++ Template/Stash.pm   Tue Jun 19 16:32:03 2001
@@ -62,13 +62,13 @@
     },
     'search'  => sub { 
        my ($str, $pattern) = @_;
-       return $str unless defined $str and $pattern;
+       return $str unless defined $str and defined $pattern;
        return $str =~ /$pattern/;
     },
     'replace'  => sub { 
        my ($str, $search, $replace) = @_;
        $replace = '' unless defined $replace;
-       return $str unless defined $str and $search;
+       return $str unless defined $str and defined $search;
        $str =~ s/$search/$replace/g;
 #      print STDERR "s [ $search ] [ $replace ] g\n";
 #      eval "\$str =~ s$search$replaceg";
--- Template/Stash/Context.pm.orig      Mon Jun 18 07:10:58 2001
+++ Template/Stash/Context.pm   Tue Jun 19 16:32:29 2001
@@ -110,13 +110,13 @@
     },
     'search'  => sub { 
        my ($str, $pattern) = @_;
-       return $str unless defined $str and $pattern;
+       return $str unless defined $str and defined $pattern;
        return $str =~ /$pattern/;
     },
     'replace'  => sub { 
        my ($str, $search, $replace) = @_;
        $replace = '' unless defined $replace;
-       return $str unless defined $str and $search;
+       return $str unless defined $str and defined $search;
        $str =~ s/$search/$replace/g;
 #      print STDERR "s [ $search ] [ $replace ] g\n";
 #      eval "\$str =~ s$search$replaceg";


Reply via email to