空の正規表現パターン (empty pattern)の挙動

恥ずかしながら初めて知ったのだが、Perl5における空の正規表現パターン(m//とかqr//とか)はだいぶトリッキーな動作をするらしい。

http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operatorsに曰く、

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 are honored; 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).

こ、これはハマりそうだな。ワンライナー的なスクリプト以外では空パターンは絶対に使わないようにした方が無難だろう。