in reply to
regex corrupting separate regex
From perlop (under "Quote-like operators"):
The empty pattern //
If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead. In this case, only the g and c flags on the empty pattern is honoured - the other flags are taken from the original pattern. If no match has previously succeeded, this will (silently) act instead as a genuine empty pattern (which will always match).
This is an odd "feature" of Perl, and it's very easy to get bitten by it in m/$pattern/
. It's happened to me several times. Perhaps a good solution is m/(?:$pattern)/
or something similar, so that the pattern is never empty.
$ perl -le 'print 1 if "foo" =~ /o/; print 2 if "asdf" =~ //'
$ perl -le 'print 1 if "foo" =~ /z/; print 2 if "asdf" =~ //'
$ perl -le 'print 1 if "foo" =~ /o/; print 2 if "asdf" =~ /(?:)/'