in reply to Re: Simple question about foreach and my.
in thread Simple question about foreach and my.

What about variables used within the loops (style, speed, etc.). Do you prefer:
foreach my $var (@array) { my $other_var = .... etc. }
or
my $other_var; foreach my $var (@array) { $other_var = .... etc. }

Replies are listed 'Best First'.
Re: Re: Re: Simple question about foreach and my.
by davorg (Chancellor) on Mar 27, 2001 at 20:29 UTC

    Depends completely on what I'm going to do with the variable. The overriding principle is that a variable is scoped as tightly as possible.

    If the variable is only used within the loop then it should be restricted to within the loop. If I'm going to use it outside the loop then I'll declare it outside the loop.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me