Prev: unintialised warning
Next: optimizar r/w en ficheros
From: C.DeRykus on 3 Nov 2009 23:00 On Nov 3, 11:18 am, Ben Morrow <b...(a)morrow.me.uk> wrote: > Quoth "C.DeRykus" <dery...(a)gmail.com>: > > > > > On Nov 2, 11:07 am, Paul Lalli <mri...(a)gmail.com> wrote: > > > > for (qw/n t f r v/) { > > > my $meta = eval("\\$_"); > > > $string =~ s/$meta/\\$_/; > > > > } > > > Did that work? I don't understand why the eval is needed > > at all: > > > my $string = "1\n 2\t 3\f 4\r 5\cK"; > > for (qw/n t f r cK/) { > > my $meta = "\\$_"; > > $string =~ s/$meta/\\$_/; > > } > > print $string; # 1\n 2\t 3\f 4\r 5\cK > > That's... evil. It relies on the fact that regexes undergo two separate > expansion phases, and requires that variable expansion happens in the > first phase but other qqish escapes are expanded in the second. I'm not > entirely convinced that's documented behaviour: anyone care to dig out > perlre and prove it one way or the other? > > For extra added evil: > > my $bs = "\\"; > $string =~ s/$bs$_/$bs$_/g for qw/n r t f/; > Perl magic is evil? Say it ain't so :) I didn't spot a full explanation in perlre but I see perlop steps through the compilation in "gory details of parsing quoted constructs" and ends with what happens at runtime in "parsing regular expressions". This closely mirrors Chapter 7's section - Perl Regular Expressions in J.Friedl's "Mastering Regular Expressions" 1st ed. -- Charles DeRykus |