in reply to Re: On the regex pattern variable to be inserted into another
in thread On the regex pattern variable to be inserted into another

> /$foo/g

I expect m/$foo/ to "recompile" because it could also be m/$foo$bar/

The point of qr is that the inside of $foo=qr// is not effected.

If you don't want it to be recompiled use $str =~ $foo without surrounding m/.../

Your problem seems to arise from the /g "global" switch which is actually changing the whole m operator and not the regex.

Please note that the documentation for qr/STRING/msixpodualn doesn't list /g !

It might help to know that m// and m//g are realized as separate commands in other languages.°

update

°) e.g. in JS

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^3: On the regex pattern variable to be inserted into another
by NERDVANA (Priest) on Feb 10, 2022 at 18:03 UTC
    Yes, but it is perfectly reasonable to expect that Perl might recognize  /$foo/ or  /$foo/g as being able to re-use a compiled expression instead of compiling a new one. This is what I think the OP was asking, and I am confirming that no, Perl does not recognize these special cases. The only way to take an unknown regex and efficiently plug it into code that does anything more elaborate than  ... =~ $foo (and gets called more than once, because it will only matter if the code gets called in a loop or something) is to eval an entire sub with the regex expanded within it.
    It might help to know that m// and m//g are realized as separate commands in other languages.

    Yes and in those languages, you can usually create an object to represent the compiled regex and then get either/both operations out of one compilation. Unfortunately perl can't do this easily.

    Edit: Perl does special-case these, actually. Just not anything more elaborate like prefixing  /\G$foo/ to change the anchor.

      > to expect that Perl might recognize /$foo/ or /foo/g as being able to re-use a compiled expression instead of compiling a new one.

      the first case maybe, tho I'm not sure about the implications of having no modifiers outside but inside.

      But how did you prove that /$foo/ is recompiling?

      Anyway as I said $str =~ $foo shouldn't! (untested)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        > But how did you prove that $foo is recompiling?

        I can't find any proof that anything is recompiled, even with //g.

        D:\>perl -E"my $x; BEGIN{$x=qr/a.*u/}; use re 'debug'; my $str = 'aou' +; say $str =~ /$x/" 1 D:\>perl -E"my $x; BEGIN{$x=qr/a.*u/}; use re 'debug'; my $str = 'aou' +; say $str =~ /$x/g" aou
        Please compare the verbose output if you replace qr// with q//

        D:\>perl -E"my $x; BEGIN{$x=q/a.*u/}; use re 'debug'; my $str = 'aou'; + say $str =~ /$x/" Compiling REx "a.*u" Final program: 1: EXACT <a> (3) 3: STAR (5) 4: REG_ANY (0) 5: EXACT <u> (7) 7: END (0) anchored "a" at 0..0 floating "u" at 1..9223372036854775807 (checking +floating) minlen 2 Matching REx "a.*u" against "aou" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [1..3] gave 2 Found floating substr "u" at offset 2 (rx_origin now 0)... ... YADDA YADDA ...

        So how do you know???

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        update

        And that's whats happening if you have more than one precompiled regex inside the match, like expected.

        D:\>perl -E"my $x; BEGIN{$x=qr/a.*u/}; use re 'debug'; my $str = 'aou' +; say $str =~ /$x$x/" Compiling REx "(?^u:a.*u)(?^u:a.*u)" Final program: 1: EXACT <a> (3) 3: STAR (5) 4: REG_ANY (0) 5: EXACT <ua> (9) 9: STAR (11) 10: REG_ANY (0) 11: EXACT <u> (13) 13: END (0) anchored "a" at 0..0 floating "ua" at 1..9223372036854775807 (checking + floating) minlen 4 String shorter than min possible regex match (3 < 4) Freeing REx: "(?^u:a.*u)(?^u:a.*u)"

        FWIW:

        This is perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x +64-multi-t hread