I'm going to answer that. But first, an introduction. Run these chunks of code as separate programs.
"japhy" =~ /a/; eval q{ print $& };
The above code prints nothing.
"japhy" =~ /a./; eval q{ print $& };
The above code prints "ap".
"japhy" =~ /a/; print $&;
The above code prints "a".

Confused yet?

Here's the worst part:

@& = (1,2,3); "japhy" =~ /a/; eval q{ print $& };
That code prints "a".


From the above examples, you should extract some things:

So what's going on? Well, the regex /a/ is too simple to be a regex, so Perl does a Boyer-Moore search instead. But the regex /a./ isn't a BM-type search, so it's a regex. And as long as it really goes through the regex engine, Perl sets up the $& functionality.

But if Perl sees you using $&, it makes everything that looks like a regex act like a regex, and set up the $& functionality. Bah. That's irritating, and "bad".

This just in: I patched gv.c so that only $& sets the PL_sawampersand flag to a true value. Now you can use @& without worry.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re (tilly) 1: Why Is $& Bad?
by tilly (Archbishop) on Sep 03, 2001 at 23:36 UTC
    Any possibility of stealing a good idea from Ruby and only calculating $`, $', and $& lazily? That way you only pay for the use you make of it...

    My guess is that it would be hard, but there is no harm asking.