On Mon, Sep 26, 2005 at 05:00:00PM -0400, David Storrs wrote:
> 
> On Sep 26, 2005, at 4:19 PM, Juerd wrote:
> >Perl 5's $& is inefficient because of this. If the variable is used
> >anywhere, Perl will for every regex used capture everything.
> 
> My understanding is that this died with 5.10.  Is that right?

$& is dynamically scoped (rather than lexically scoped).
I don't believe that it's possible to avoid capturing it anywhere, without
affecting correctness somehow.

There is already an optimisation to avoid capturing it if $& is never seen,
but even that is actually buggy:

$ perl -lwe '$_ = "trouble"; /o/; print eval q.$&.'
Use of uninitialized value in print at -e line 1.

compare with

$ perl -lwe '$&; $_ = "trouble"; /o/; print eval q.$&.'
Useless use of a variable in void context at -e line 1.
o

where by mentioning $& I set the "seen $& somewhere" flag, so $& is
captured, and it's there when the '' eval gets compiled at run time.

IIRC the perl 6 equivalents of $` $& and $' are all lexical rather than
dynamic, so the pain will be far less. (at least in its scope)

Nicholas Clark

Reply via email to