in reply to Re: Re: What Is Going On Here ?????
in thread What Is Going On Here ?????

$_ is a global variable. It is best to save it for inner loops, but if you must do it like this give it a seperate value for the scope of do_something with local($_).

Updated: Okay thanks for correcting me extremly, but it seem to me to make more sense (and is clearer) if $_ is saved for innermost loops.
For example this could be clearer:
while (<>) { for (split /-/,$_) { print $_ . "\n"; } }

Replies are listed 'Best First'.
Re: Re: Re: Re: What Is Going On Here ?????
by extremely (Priest) on Nov 22, 2000 at 05:02 UTC
    Um this isn't exactly true... Run this... $_ isn't GLOBAL, it is LOCALized automatically. The inner and outer loops each have their own $_ but the "current" $_ is still in scope in the supbroutine called...
    #!/usr/bin/perl -w use strict; my @l=qw( a b 3 d ); foreach (@l) { print "$_ = "; foreach (@l) { print "$_ "; } print "\n"; } foreach (@l) { icky(); } sub icky { print "$_ = "; foreach (@l) { print "$_ "; } print "\n"; }

    You don't need to local($_) inside a foreach loop.

    --
    $you = new YOU;
    honk() if $you->love(perl)