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


In reply to \U, \E and friends interpolation in regex operators by Hue-Bond

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.