in reply to how to use string RE (against $_ and capture)
In your first code sample, you have:
my $re_str=qr{([0-6BS])}; $_="B"; my $ans; if (eval m{$re_str})
Why are you using eval? It should not be needed.
In your second code sample:
my $re_str="qr{([0-6BS])}"; $_="B"; my $ans; if (eval m{$re_str})
The m{$re_str} will be interpreted as m/qr{([0-6BS])}/ which is looking to match something that starts with "qr". (And I'm not sure how the "{([0-6BS])}" will get interpreted as. "{" and "}" in a regex are normally used to quantify the number of matches.)
To store a regex as a hash key, you could prefix the regex with a character that won't otherwise appear as the first character of any of your hash keys. So, something like this:
#not tested, YMMV for $key (keys %options) { if ($key =~ s/^#//) { if (/$key/) { print "Match: $1\n"; } } else } if ($_ eq $key) { print "Equals: $1\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to use string RE (against $_ and capture)
by AnomalousMonk (Archbishop) on Nov 12, 2016 at 00:15 UTC |