in reply to Re^2: Composing regex's dynamically
in thread Composing regex's dynamically

Regexes and strings are interchangeable. "Joining" two regexes (by interpolating them into a third) will join their stringified versions.

If you want to "compile" a string to a regex, use qr() on the final string, like I did.

Replies are listed 'Best First'.
Re^4: Composing regex's dynamically
by John M. Dlugosz (Monsignor) on Apr 27, 2011 at 15:41 UTC
    One nice thing about Perl that's not present in other languages is that Regex is handled intrinsically rather than having to be a plain string first. That means, in particular, that we don't have to double-escape everything. \{ in a string literal becomes plain {, so you would have to write \\{ to get what you would normally write as \{ in a regular expression literal.

    So, you are saying that I can write the pieces as qr's (not plain strings), and the stringized form has everything escaped out so if I compile it again I get back the same regex?

    If I can mix qr literals and strings more freely without worrying, it will indeed be simpler.

      See perlop on qr:

      It (the result of qr) magically differs from a string containing the same characters: ref(qr/x/) returns "Regexp", even though dereferencing the result returns undef.

      Basically, the difference between strings and regular expression objects is very, very thin from a user point of view.

        I read that sentence, but didn't take-away the same thing.

        But a string containing the same characters would not have the same meaning! ... Hmm, playing around a little, I see: "a string" value is not the same as a string literal. Once it's in a string, the backslashes in particular are already understood.

        So, assuming the line in the docs is not written with the precision of an ISO standard, the intent is that the stringified regexp should contain whatever is necessary to reproduce the same meaning if compiled again?