For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > \Q\E fails with '$' ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author \Q\E fails with '$' ?
Tom Arnall

2006-03-23, 3:58 am

from the debugger:

p 1 if /[\Q$\E]/

gets:

Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE E\]/

the following works:

p 1 if /[\Q\$\E]/

but shouldn't \Q \E be sufficient to make perl treat '$' as a literal?

thanks,

tom arnall
north spit, ca

John W. Krahn

2006-03-23, 7:58 am

tom arnall wrote:
> from the debugger:
>
> p 1 if /[\Q$\E]/
>
> gets:
>
> Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE E\]/
>
> the following works:
>
> p 1 if /[\Q\$\E]/
>
> but shouldn't \Q \E be sufficient to make perl treat '$' as a literal?


No, interpolation of variables happens before the escape sequences so that the
contents of the variables can be quotemeta()ed:

$ perl -le' $x = q[A.C]; $y = qr[\Q$x\E]; print $y'
(?-xism:A\.C)
$ perl -le' @x = qw[A . C]; $y = qr[\Q@x\E]; print $y'
(?-xism:A\ \.\ C)

By default the $\ variable contains the string "\n":

$ perl -le' $y = qr[\Q$\E]; print $y'
(?-xism:\
E)

So after the variable is interpolated that is seen as /\Q\nE/.



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com