Hue-Bond has asked for the wisdom of the Perl Monks concerning the following question:
I've just read section 5.9.2.1 "When backslashes happen" of "Programming Perl" and are having trouble getting it. I think.
If I understand correctly, interpolation in regexes happens in two phases: the first one is a normal double-quotish interpolation (if allowed by the regex delimiters) and the second is made by the regex engine. The first one defers most of the work in order to prevent the engine from seeing \t as a space and trimming it if the /x modifier is specified (among other things, I suppose). The engine doesn't understand \u, \U, \l, \L, \E and \Q, and it takes them literally.
So m'\u$var\t'; matches a string contaning a backslash, the character 'u', a dollar sign, the string 'var' and a tab. Is this right?
And m/\u$var\t/; matches whathever is the value of $var (with its first character capitalized) followed by a tab. And the tab is expanded by the regex engine, not by the quote interpolation, isn't it?
But then:
$ perl -Mstrict -w print my $var = '\utext'; "Text" =~ m/$var/ && print "matches\n"; __END__ Unrecognized escape \u passed through in regex; marked by <-- HERE in +m/\u <-- HERE text/ at - line 2. \utext$ _
Why is not $var interpolated in this regex? Maybe because Perl won't do a double-quotish interpolation twice? (one pass for turning '$var' into '\utext' and another one for translating '\utext' into 'Text').
--
David Serrano
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: \U, \E and friends interpolation in regex operators
by davido (Cardinal) on Dec 14, 2005 at 03:58 UTC | |
|
Re: \U, \E and friends interpolation in regex operators
by l.frankline (Hermit) on Dec 14, 2005 at 04:15 UTC |