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 | |
by hilitai (Monk) on Mar 11, 2018 at 20:23 UTC | |
by haukex (Archbishop) on Mar 12, 2018 at 12:36 UTC | |
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 | |
Re: Is there any way to put regex modifiers into a variable?
by LanX (Saint) on Mar 11, 2018 at 18:56 UTC | |
Re: Is there any way to put regex modifiers into a variable?
by bart (Canon) on Mar 12, 2018 at 08:17 UTC | |
Re: Is there any way to put regex modifiers into a variable?
by Anonymous Monk on Mar 12, 2018 at 14:13 UTC |
Back to
Seekers of Perl Wisdom