The stupid question is the question not asked | |
PerlMonks |
Guess I don't have \Q and \E figured out yet...by Guildenstern (Deacon) |
on Apr 30, 2001 at 18:42 UTC ( [id://76599]=perlquestion: print w/replies, xml ) | Need Help?? |
Guildenstern has asked for the wisdom of the Perl Monks concerning the following question:
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.
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:
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!
Back to
Seekers of Perl Wisdom
|
|