Friday, 13 July 2007

$* replacement

So, the special variable $* has been removed from perl 5.10, after having been deprecated for years. For those who don't remember Perl4-style programming, $* was used to enable multi-line matching in regular expressions. In Perl 5, the preferred way to do it is to use the more flexible regexp flag /m.

I removed that poor old variable not because I like removing old things, but because it was standing in the way of a bug I wanted to fix (bug #22354). Anyway. Apparently there is still out there some old Perl code that fears not using $*. And notably ghc's evil mangler, which broke with perl 5.9.5.

But Audrey Tang (who else?) found an elegant way to emulate $* = 1, in a characteristic perl-zen manner. Here's how: the current evil mangler contains this simple line of code, in a BEGIN block:

require overload; overload::constant( qr => sub { "(?m:$_[1])" } );

1 comment:

Audrey said...

However, that turns out to break horribly when you interpolate into regexes: /...$bar.../ will compile the two /.../ as /(?m:...)/, but $bar will still retain the vanilla semantics.

(This also affects Regexp::DefaultFlags and makes Damian's auto-flags idea more fragile than previously thought.)

In any case, I ended up wrote a three-liner with PPI and appended /m on all s/// and m// constructs. :-)