> 
> Are there performance implications for using the implicit variable ($_) rather
> than declaring a specific variable?  If so how great ?

Yes, though very minimal performance hit if anything.

Consider the following:

while (chomp(my $line = <file>)) { ... }
vs.
while (<file>)( ... }

According to an excellently written piece on http://www.perl.com a while
back, entitled something like "10 red flags"  ( I really should go look
it up, but it's late ) One of the warnings for performance was to avoid
variables is you can.

In the first example, perl has to fill $_ AND $line with the line read
from the filehandle <file>.  In the second example, it only needs to
fill $_  (which it's going to either way).

*note* I'm not a regular browser of the source, but I believe that perl
populates both variables based on my tests *note*.

Now, this is an incredibly small issue of performance, the amount of
memory saved by avoiding the use of a named variable pales in comparison
to the amount of memory on most systems these days.  The only time you
might consider so few bytes important is on really obselete hardware or
under mod_perl.

Final Answer?  Yes there is a performance difference, and in 95% of
cases it's utterly irrelevant.

> Also, are there times when one is preferred over another - or is it simply the
> coder's preference?

Some people perfer the readability of "while (<IN>) { print } "over the
alternative of
"while ($line = <IN>) {print $line} " and some people prefer the
opposite.  Python programmers will tell you that #2 is the _only_ way to
go, yet the perl individualist will believe that whatever suits the
needs of your project are the most important.

Personally, since I work mostly under mod_perl these days, I use the
result that leaves a) the least characters in the file and b) uses the
most effecient use of memory and CPU.


--A

Reply via email to