http://qs1969.pair.com?node_id=1210675

hilitai has asked for the wisdom of the Perl Monks concerning the following question:

I want to put the modifier string to a regular expression into a variable, so that the modifiers can be selected at runtime. However, my naive attempt does not work:

#!/usr/bin/perl -w use strict; my $opts = "io"; my $regex = "test"; my $file = "./testfile"; open(my $fd, '<', "$file") or die "$!"; my @ary = <$fd>; close $fd; for my $line (@ary) { print $line if ($line =~ /$regex/$opts); } > ./test.pl test.txt Scalar found where operator expected at ./test.pl line 13, near "/$reg +ex/$opts" (Missing operator before $opts?) syntax error at ./test.pl line 13, near "/$regex/$opts"

...and I've tried wrapping the comparison in an eval block, but to no avail. Is making the regex modifiers ... modifiable possible?

Thanks in advance for any hints, slaps upside the head, etc.

Replies are listed 'Best First'.
Re: Is there any way to put regex modifiers into a variable?
by davies (Prior) on Mar 11, 2018 at 19:06 UTC

    You can do it slightly differently by putting the regex modifier(s) inside the regex:

    use strict; use warnings; my $opts = "io"; my $regex = "test"; while (<DATA>) { my $line = $_; print $line if $line =~ /(?$opts)\Q$regex\E/; } __DATA__ untested unknown

    Note that this reveals a slight problem - I can't find your 'o' modifier in the docs. Note also that I have wrapped the regex itself in \Q\E to prevent problems if it contains metacharacters.

    Regards,

    John Davies

      Yes! Thank you! That is the slap upside the head that I needed.

      With regards to the /o modifier, see the Perlop man page, Regexp Quote-Like Operators.

        the /o modifier

        Although I'm having trouble finding it at the moment, I think there was some recent discussion that /o is often not what you want. All I found so far is /o is dead, long live qr//!, although that's not recent. There's the FAQ What is /o really for?. You could perhaps look into qr//.

Re: Is there any way to put regex modifiers into a variable?
by AnomalousMonk (Archbishop) on Mar 11, 2018 at 19:55 UTC
Re: Is there any way to put regex modifiers into a variable?
by Discipulus (Canon) on Mar 11, 2018 at 19:45 UTC
    Hello,

    for sure davies's answer is wiser but maybe you are interested to know that also the feared eval string can accomplish the taske:

    perl -e "$opts = 'io'; $rex = eval qq(qr/aka/$opts); print 'match' if +'ALAKAIA' =~ $rex" match

    PS infact eval is powerful but dangerous: consider at least to check what was passed in following the good principle never trust user input that can be also read as never trust user or just never trust ;=)

    my $opts = $ARGV[0]; die "never trust user input" unless $opts=~/^[msixpodualn]+$/; ...

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Is there any way to put regex modifiers into a variable?
by LanX (Saint) on Mar 11, 2018 at 18:56 UTC
    Modifiers decide how the regex is compiled, so it must happen at compile time. *

    Though putting it inside an eval string ( not block!) should work.

    What did you try?

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

    *) I think I was wrong.

Re: Is there any way to put regex modifiers into a variable?
by bart (Canon) on Mar 12, 2018 at 08:17 UTC
     print $line if ($line =~ /$regex/$opts);
    Make that
    print $line if ($line =~ /(?$opts:$regex)/);
Re: Is there any way to put regex modifiers into a variable?
by Anonymous Monk on Mar 12, 2018 at 14:13 UTC
    You can compile an arbitrary string into a variable which contains a regular-expression. The value of this variable can be subsequently used to match things. See qr// under perldoc perlop ("Regexp Quote-Like Operators"). **EDIT** "qr" not "qx!" Oops.