I ran across a substitution problem the other day that quite baffled me. I'll give a very simple case here as an example. I have a hash whose values are entries to search for and whose values are what to replace with.
my %sub_hash = ( "&somechar;" => "\a111" );

Having been recently apprised of the use of \Q and \E in REs, I knew my substitution statement would have to look something like:
$text =~ s/\Q$foo\E/$sub_hash{$foo}/ig;
The result of this substitution was not the end result that I was looking for, but it was easy to tell what had gone wrong. My replaced text became ox07 (the ASCII bell) followed by the 111. Simple enough to fix, I thought, since it was obvious that the \a portion of the replacement was being converted into the bell character. So I revised my RE to look like this:
$text =~ s/\Q$foo\E/\Q$sub_hash{$foo}\E/ig;
The results of this RE were a bit more confusing. Instead of ending up with \a111 in the replacement text, I got \(ASCII bell)111. The backslash was now showing up, but somehow the ASCII bell character was still being inserted.
Finally, I just changed the values in the hash to look like this:
my %sub_hash = ( "&somechar;" => "\\a111" );

I then reverted back to my original RE, and everything worked as expected.

Why did the \Q and \E in my second RE appear to work incorrectly? I can understand the \a being interpreted as the ASCII bell without the \Q and \E modifiers, but why the a by itself was interpreted as the bell when using the modifiers is beyond me. I hesitate to say that I've found a bug, since it's far more likely that I've simply missed something in my learning of REs.

Guildenstern
Negaterd character class uber alles!

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.