Skip Menu |

This queue is for tickets about the Template-Toolkit CPAN distribution.

Report information
The Basics
Id: 107797
Status: resolved
Priority: 0/
Queue: Template-Toolkit

People
Owner: Nobody in particular
Requestors: OKLAS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 2.26
Fixed in: (no value)



Subject: regexp with capture is not worked as expected
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
From: ozcoder [...] gmail.com
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
RT-Send-CC: ozcoder [...] gmail.com
Срд Фев 03 02:05:32 2016, ozcoder@gmail.com писал: Show quoted text
> Hi, > > On Thu Oct 15 05:06:19 2015, OKLAS wrote:
> > 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
Thanks for answer. I am not forgot to escape the $1 i.e. \$1. I am read the documentation example at this link: http://search.cpan.org/perldoc?Template::Manual::VMethods#replace%28search,_replace%29 It is documentation bug. Documentation example work fine with yours escape fixing and work wrong without it. Is need to open another ticket for it or leave this open?
From: ozcoder [...] gmail.com
Hi Seguel, On Fri Feb 05 04:39:32 2016, OKLAS wrote: Show quoted text
> Срд Фев 03 02:05:32 2016, ozcoder@gmail.com писал:
> > Hi, > > > > On Thu Oct 15 05:06:19 2015, OKLAS wrote:
> > > 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
> > > Thanks for answer. I am not forgot to escape the $1 i.e. \$1. I am > read the documentation example at this link: > http://search.cpan.org/perldoc?Template::Manual::VMethods#replace%28search,_replace%29 > > It is documentation bug. Documentation example work fine with yours > escape fixing and work wrong without it. > Is need to open another ticket for it or leave this open?
Yes, it's not explained very well, but I'm not sure how to make it better. At the moment it says "If you use double quotes then TT will try and interpolate the variables before passing the string to the replace vmethod." But we are using double quotes for the Template->new->process call, even though we are passing a reference to a string \" and so you need to escape the $1 as \$1. Gordon
Hi Gordon, It is posiible to exclude and abstract from escaping. The replace function must be identical to pure perl regexp. But it is not identical. You are wrote: Show quoted text
> If you use .+ instead it works properly. > If you anchor somehow '^' or 'n' it works properly.
The question is: Why need to use '^' at template and does not need at pure perl regexp? compare: [% s=s.replace('^(.*)','$1.txt'); %] $s =~ s/(.*)/$1.txt/; What is the rule of this?
Ticket migrated to github as https://github.com/abw/Template2/issues/171