Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

fixstyle script in PERL COOKBOOK

by masterhenry (Initiate)
on Apr 14, 2001 at 00:54 UTC ( #72481=perlquestion: print w/replies, xml ) Need Help??

masterhenry has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm having some trouble with the fixstyle script provided by the Perl CookBook. It works fine when I use regular words to do global search and replace. But when I try to replace with, it gives an error saying that I need to escape @. But when I try to put webmastr\ and allezmail\, then it runs, BUT it doesn't make the replacement of to I'm thinking that it's not properly escaping the @. Am I doing something wrong? Is there another way to escape the @ that I'm not aware of? Please tell me. Thanx. It would be helpful if you email me at

P.S. The line that is giving the error is when I put: =>
It works when I put:
hello => hi
but not with the @

Replies are listed 'Best First'.
Re: fixstyle script in PERL COOKBOOK
by sachmet (Scribe) on Apr 14, 2001 at 02:09 UTC
    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;
    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.
      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.

Re: fixstyle script in PERL COOKBOOK
by rchiav (Deacon) on Apr 14, 2001 at 02:15 UTC
    I just pulled out The CookBook and I think I see your problem. Have you tried to just escape the @ in the expression that you want to replace the first with? It looks like the string you're searching for is quoted with \Q \E so it's interpreting your \ in the first expression as a real \.

    Hope this helps.. Rich

      thanx... it didn't work... but I did find the answer from the person that replied right above you. But thanx for your efforts... =)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://72481]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-05-18 01:23 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (171 votes). Check out past polls.