Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Guess I don't have \Q and \E figured out yet...

by hdp (Beadle)
on Apr 30, 2001 at 18:56 UTC ( [id://76605] : note . print w/replies, xml ) Need Help??


in reply to Guess I don't have \Q and \E figured out yet...

This particular problem has little to do with regexes and/or \Q. \a in a doublequoted string will be interpreted as 0x07; the reason your fix works is because you're backslashing the \.

You didn't actually have "the a by itself" -- by the time your string gets to the regex, \a has already been interpreted as a beep, and so applying \Q leaves you with a backslashed beep. In fact, anything *but* \\a in the original string constant (e.g. using \Q or quotemeta) will be too late to do anything but leave you with a backslashed beep.

hdp.

Replies are listed 'Best First'.
\Q and \E vs. quotemeta?
by Albannach (Monsignor) on Apr 30, 2001 at 20:47 UTC
    OK, that seems clear hdp, but I'm confused by the following snippet (using \t instead of \a for visibility) in which perlfunc:quotemeta appears to behave differently than /Q/E:
    $foo = "\tfoo"; $_ = ">>ha<<\n"; s/ha/$foo/; print; $_ = ">>ha<<\n"; s/ha/\Q$foo\E/; print; quotemeta $foo; # update: this is doing nothing! $_ = ">>ha<<\n"; s/ha/$foo/; print; $foo = '\tfoo'; $_ = ">>ha<<\n"; s/ha/$foo/; print; $_ = ">>ha<<\n"; s/ha/\Q$foo\E/; print; quotemeta $foo; # update: this is doing nothing! $_ = ">>ha<<\n"; s/ha/$foo/; print;
    which produces:
    >> foo<< >>\ foo<< >> foo<< >>\tfoo<< >>\\tfoo<< >>\tfoo<<
    What am I forgetting here?

    Update Aarghhh! How bloody embarrassing! Thanks tye, that's what I get for minimizing my keystrokes!
    Useless use of quotemeta in void context at D:\Perl\tmp\quotemeta.pl line 9.
    Useless use of quotemeta in void context at D:\Perl\tmp\quotemeta.pl line 15.
    /me smacks himself in the forehead

    --
    I'd like to be able to assign to an luser

      $foo= '\t\a\b\c'; print "foo=($foo)\n"; quotemeta $foo; print "foo=($foo)\n"; $foo= quotemeta $foo; print "foo=($foo)\n"; __END__ Produces: foo=(\t\a\b\c) foo=(\t\a\b\c) foo=(\\t\\a\\b\\c)

      That is, quotemeta doesn't modify in place, it returns a modified value so your uses of quotemeta are useless.

      If you had turned on warnings, then you would have been told: Useless use of quotemeta in void context at quotemeta.pl line 5. which is why we often urge people to do stuff like that. q-:

              - tye (but my friends call me "Tye")