Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$& is evil - Discuss 1

Status
Not open for further replies.

MikeLacey

MIS
Nov 9, 1998
13,212
GB
In another thread icrf made the comment that $& is "evil" which, at the very least, is perhaps a little - well, extreme? :)

Now being lazy, I use this kind of construct to work my way through a string all of the time; like this:

$str = 'Mike_Lacey_is_a_lazy_programmer';

I find the first delimiter, an underscore character in this case, by searching for a _

/_/;

and that gives me the following values in some Perl pre-defined variables:

$` is set to 'Mike', everything before what was found.

$' is set to 'Lacey_is_a_lazy_programmer', everything *after* what was found.

and $& is set to what was actually found by the regex, '_' in this case.

The other approach, which I find difficult to read to be honest, would be to do something like this.

($wrd1,$wrd2,$wrd3,$wrd4,$wrd5,$wrd6) =
/(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)/;

or

/(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)/;
$wrd1 = $1;
$wrd2 = $2;
$wrd3 = $3;
$wrd4 = $4;
$wrd5 = $5;
$wrd6 = $6;

I do this so infrequently I've probably got the syntax wrong, so ignore that if I have please.

So - why is the second approach so popular? Is it more efficient? Does it just fit better with the way most people program?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I don't which other thread you're referencing here, but from from what I remember from the perl cookbook $&, $` and $' were considered to be an expensive operator so that their presence anywhere slows down all and any pattern matching in the complete script (as the perl has to find these and put these which by default it doesn't)

but then at least $& is no longer as expensive in current version of perl, so its just a matter of habit now.


---
cheers!
san
pipe.gif


"The universe has been expanding, and Perl's kind of been expanding along with the universe" - Larry Wall
 
I think its just style. Seems they want to list the vars out so its easier to read.
 
Actually, according to perldoc for perl 5.8.0, the entry for $& in perlvar ( states the following:

"The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. See BUGS"

BUGS section states:

"Due to an unfortunate accident of Perl's implementation, use English imposes a considerable performance penalty on all regular expression matches in a program, regardless of whether they occur in the scope of use English. For that reason, saying use English in libraries is strongly discouraged. See the Devel::SawAmpersand module documentation from CPAN ( ) for more information.

Having to even think about the $^S variable in your exception handlers is simply wrong. $SIG{__DIE__} as currently implemented invites grievous and difficult to track down errors. Avoid it and use an END{} or CORE::GLOBAL::die override instead."
 
Performance penalty then - ok...


Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I was under the impression that the performance impact was also present when using parentheses - so either method would degrade performace.

Scotty
 
Ah! I have just read the FAQ - the way I interperate it is as follows:

Once you use $&, $` or $' in a regexp, it affects the speed of *all* other regexp's.

Use of parentheses '()' in a regexp *only* affects the speed of that regexp.

There is a futher note that using $& is now less expensive than $` or $' which was pointed out by san.

Scotty
 
Why not use

@x = split "_", "i_am_lazy_too_you_see";
print join "-", @x
 
<grin>

oh no -- *way* too simple

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top