On Mon, Apr 23, 2001 at 12:01:02PM +1000, [EMAIL PROTECTED] wrote:
: 
: 
: Hello Andy,
: 
: Are there performance implications for using the implicit variable ($_) rather
: than declaring a specific variable?  If so how great ?

There is no performance penalty that will be noticable.  Note that
Perl is generally expecting to use $_ by default.

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

Coder's preference, however, I must say that for me, using $_ is much
more readable.  I understand what it is used for and when so I expect
it.  If the use of $_ makes the code less readable I say don't use
it.  Note the example of nested loops:

foreach ( 1 .. 10 ) {
  foreach ( 1 .. $_ ) {
    # in here, $_ refers to the inner most loop's looping variable, no
    # good.
  }
}

try instead:

foreach my $i ( 1 .. 10 ) {
  foreach my $j ( 1 .. $i ) {
    printf "%3d => %3d", $i, $j;
  }
}

much better, and readable.



-- 
Casey West

Reply via email to