On Tue, Jun 19, 2001 at 09:39:13PM -0400, Eric Beaudoin wrote:
> At 16:39 2001.06.19, Jeff 'japhy' Pinyan wrote:
> >This is because the looping variable is implicitly localized to the loop
> >itself.  This is not a bug.
> 
> Has this always been the case? I was under the impression that the syntax 
> 
>         for my $line (1 .. 10) { ; }
> 
> was introduced to allow an easy way to localize the iteration variable. It
> doesn't make mush sense if the itaration variable is localized by default.

I believe the localized iterator has always existed, at least in Perl 5, but
I have not verified this belief.  Delving into the various perldeltas will
tell you one way or the other.

However, regardless of whether or the iterator has always been localized,
the foreach syntax -has- made it easier to localize a variable.  Consider:

    use strict;

    my $foo;
    foreach $foo (...) { ... }

OR

    use strict;

    {
        no strict;
        foreach $foo (...) { ... };
    }

OR

    use strict;
    use vars qw($foo); # a global iterator, even

    foreach $foo (...) { ... }
    
AGAINST

    foreach my $foo (...) { ... }


The alternative, of course, is to run with no strict, then it's just as
short.  I don't think I have to mention how bad an idea that is.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to