in reply to Regexp substitution using variables

For certain flags you can do:

$value =~ s/(?$flags:$pattern)/$replacement/;

Replies are listed 'Best First'.
Re^2: Regexp substitution using variables
by Bod (Parson) on Nov 25, 2020 at 23:17 UTC

    This answer and other answers have suggested:

    $value =~ s/(?$flags:$pattern)/$replacement/;
    In my answer I used a subtle variation:
    $value =~ s/(?$flags)$pattern/$replacement/;
    which worked as expected in my test code. So I went off to the documentation and sure enough it shows both but it does not (at least to my eyes) show what the difference is between them. Can anyone explain if there is a difference and when it practically matters? It doesn't seem to matter here.

    On a different note - is it preferred by other Monks that questions like this get asked in the thread or is the preference for them to have their own new thread?,

      > does not (at least to my eyes) show what the difference is between them.

      you are comparing

      in your example there is no difference, but in the second approach with pattern the reach of modifiers is limited to the group.

      DB<24> p 'xX' =~ /(?i:X)X/ 1 DB<25> p 'xX' =~ /(?i:X)x/ DB<26> p 'xX' =~ /(?i)Xx/ 1 DB<27>

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