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

Hi could someone please enlighten me on this:
$code .= "s{\\Q$in\\E}{$out}g";
What do the \Q and the \E mean? This is from a script in the Perl Cookbook... Thanks monks!
Update:
From the Camel book page 61, \Q is to "Backslash all following nonalphanumeric characters" and \E is to End the expression. Am I right in understanding that \Q is to "Escape whatever that follows, including special characters like $, \ etc" ? Thanks, and sorry for replying to my own question... sheesh the trials and tribulations of a Perl newbie... :-\
Update:
Edited title, following davido's editorial feedback...

Replies are listed 'Best First'.
Re: What do \Q and \E mean?
by ikegami (Patriarch) on Jun 19, 2005 at 02:51 UTC

    What's between \Q and \E is treated as normal characters, not regexp characters. For example,

    '.' =~ /./; # match 'a' =~ /./; # match '.' =~ /\Q.\E/; # match 'a' =~ /\Q.\E/; # no match

    It doesn't stop variables from being interpolated.

    $search = '.'; '.' =~ /$search/; # match 'a' =~ /$search/; # match '.' =~ /\Q$search\E/; # match 'a' =~ /\Q$search\E/; # no match

    By the way, I think you want
    "s{\\Q\$in\\E}{\$out}g"
    instead of
    "s{\\Q$in\\E}{$out}g"
    The difference will be seen when $in or $out contains a '$'.

      Thanks for your nice explanation, ikegami.
      By the way, I think you want "s{\\Q\$in\\E}{\$out}g" instead of "s{\\Q$in\\E}{$out}g" The difference will be seen when $in or $out contains a '$'.

      Yes, it is evident from your explanation that that would be the case. I did not post the rest of the code, but my $in and $out are a finite set of strings, neither containing a '$'.
      Thanks guys, for your lightning fast responses. I thought I had it figured out in a few minutes, and by the time I updated my entry, I see three replies!! Way cool. Can you tell me how you keep track of who posts new questions, that enables you to respond so quickly... Thanks again. PM rocks. :)
        I just happened to check Perl Monks for the first time tonight at the right time. We also reload Perl Monks a fair bit, but there's no notification system (except for replies to your own posts).
        I tend to watch [id://Newest Nodes] and look at the top of the list. Even doing this, I often see replies in only minutes after a post. You might also find [id://Recently Active Threads].
Re: What do \Q and \E mean?
by mifflin (Curate) on Jun 19, 2005 at 02:47 UTC
    In a regular expression, all chars between the \Q and \E are escaped. See quotemeta in perlfunc.
    In your code sample, you are appending a string, that looks like a regular expression, onto the end of $code.
    update
    I couldn't find my Perl Cookbook, must have left it at work, however, the code you are referring to is probably assembling a regex so it then can eval it. I seem to remember it was showing some way of performing efficient variable regex's.
      Well, the code is from a script that for an input file, translates a string found in a set of strings ($in) to a corresponding string found in another set of strings ($out), which have been created elsewhere, with appropriate mappings. Example 1.4 from the Perl Cookbook. Sorry for not posting more code, just wanted clarification on those weird (now clear) \Q and \E switches.
Re: What do \Q and \E mean?
by tlm (Prior) on Jun 19, 2005 at 02:48 UTC

    In general \Q...\E is used in double-quote context (such as one gets inside m// and the first leg of s///) to quote characters (such as * and +) that would otherwise have special meaning in a regular expression. E.g.:

    'aaaa+' =~ /(a+)/; print "$1\n"; 'aaaa+' =~ /(\Qa+\E)/; print "$1\n"; __END__ aaaa a+
    The first regexp matches the string of a's, whereas the second one matches the substring 'a+'.

    the lowliest monk