Oooch... You are right, not only that, I have a subtle bug in something I was doing 
where I thought that worked. =/

How about this:
1. Look at FILTER
2. Add this into Stash.pm:
$SCALAR_OPS = {
+     'substr'  => sub {
+       my ($str, $offset, $length) = @_;
+       return $str unless defined $str and defined $offset;
+       return substr($str, $offset) unless defined $length;
+       return substr($str, $offset, $length);
+     },
    'item'    => sub {   $_[0] },
    'list'    => sub { [ $_[0] ] },
    'hash'    => sub { { value => $_[0] } },

=) I'm working this up as a patch to add the new pseudo method...
array.substr(0,1) will get you the first char easily.


> -----Original Message-----
> From: Joerg, Harald [mailto:[EMAIL PROTECTED]]
> Sent: Friday, January 25, 2002 7:03 AM
> To: Mark Mills
> Cc: '[EMAIL PROTECTED]'
> Subject: RE: [Templates] How to get the first character of a string?
> 
> 
> > > Simple, but does not work:
> > >     [% text.replace('(.).*','$1') %] expands to '$1'
> > 
> > I think that needs to have doublequotes around the "$1" if 
> you want it to interpret.
> > 
> > [% text.replace('^(.).*',"$1") %]
> > 
> Did you try that? I did, and I get
> 
>   ! file error - parse error: xyzzy.html line unknown: 
> unexpected token (1)
>   [% $1 %]
> 
> This comes as no surprise for me since TT interpolates double
> quotes as perl does - but there is no TT variable '1'.
> 
> It is not so easy to get $1 to work in the way required here
> with the replace SCALAR_OP since perl does only one
> level of variable interpolation. 
> The crucial line in context.pm reads
> 
>     $str =~ s/$search/$replace/g;
> 
> ...so $replace is interpolated by my parameter, but the $ in $1
> is not interpolated. I replace my search string with literal '$1'.
> -- 
> Cheers,
> haj
> 


Reply via email to