Hi,
On Thu Oct 15 05:06:19 2015, OKLAS wrote:
Show quoted text> Ther out of this perl code:
>
> perl
> my $s='name'; $s =~ s/(.*)/$1.txt/; print $s;
>
> is:
>
> name.txt
>
> But expected Template Toolkit equivalent:
>
> perl -MTemplate
> Template->new->process(
> \"[%s='name';s=s.replace('(.*)','$1.txt');s%]"
> );
>
> is:
>
> .txt.txt
>
Firstly, you forgot to escape the $1 i.e. \$1
The weird thing is because .* matches zero or more characters it is doing both. Matching more characters giving 'name.txt' then matching zero characters giving '.txt' for a total of 'name.txt.txt'. Weird.
If you use .+ instead it works properly. If you anchor somehow '^' or 'n' it works properly.
$ perl -Iblib/lib -Iblib/arch -MTemplate -demo
Loading DB routines from perl5db.pl version 1.37
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(-e:1): mo
DB<1> Template->new->process(\"[% s='name';s=s.replace('(.*)','\$1.txt');s %]")
name.txt.txt
DB<2> Template->new->process(\"[% s='name';s=s.replace('(.+)','\$1.txt');s %]")
name.txt
DB<3> Template->new->process(\"[% s='name';s=s.replace('^(.*)','\$1.txt');s %]")
name.txt
DB<4> Template->new->process(\"[% s='name';s=s.replace('(n.*)','\$1.txt');s %]")
name.txt
Gordon