Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: fixstyle script in PERL COOKBOOK

by sachmet (Scribe)
on Apr 14, 2001 at 02:09 UTC ( #72504=note: print w/replies, xml ) Need Help??


in reply to fixstyle script in PERL COOKBOOK

That is odd...
#!/usr/bin/perl $string = "a\@b.c\n"; $string =~ s#\Qa@b.c\E#email-address#g; print $string; $string2 = "a\@b.c\n"; $string2 =~ s#a\@b\.c#email-address#g; print $string2; $string3 = "a\@b.c\n"; $qm3 = quotemeta('a@b.c'); $string3 =~ s#$qm3#email-address#g; print $string3;
Output:
a@b.c email-address email-address
So it seems that method won't work correctly. Perhaps another Monk can enlighten.

Corion suggests that \Q$var\E should work as normal; perhaps change the line that reads:
$code .= "s{\\Q$in\\E}{$out}g"
to read:
$code .= "\$var=quotemeta q{$in}; s{\$var}{$out}g"
for all to be well with the world. And it appears to, as well.

Update:D'oh, messed up the replacement line,fixed
Update:Corion once again points out that the reason for this is because @b gets interpolated before \Q takes effect. I should have realized this was the case.

Replies are listed 'Best First'.
Re: Re: fixstyle script in PERL COOKBOOK
by Anonymous Monk on Apr 20, 2001 at 23:05 UTC
    Thanks a lot !! It works... But I also had to escape the @ in the output. By the way, I don't know what \Q \E means... could you tell me why? Thanks...
      They are explained in perlre.

      What \Q does is disable a lot of the "metacharacters" in REs until the next \E. This is often important when substituting variables in an RE because you have little control over the contents of the variable.

      An alternate solution is to quote all possible meta characters with quotemeta.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://72504]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-04-19 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?