Sam Tregar
# On Thu, 8 Nov 2001, Dan Sugalski wrote:
#
# > Gack. Looks like a mis-placed optimization in perl 5. The
# list of a foreach
# > is *supposed* to flatten at loop start and be static.
# Apparently not. :)
# >
# > Care to file the perl 5 bug report, or shall I?
#
# It's not a bug.  Check out the "Foreach Loops" section in
# perlsyn, where
# you'll find:
#
#        If any element of LIST is an lvalue, you can modify it by
#        modifying VAR inside the loop.  Conversely, if any element
#        of LIST is NOT an lvalue, any attempt to modify that
#        element will fail.  In other words, the "foreach" loop
#        index variable is an implicit alias for each item in the
#        list that you're looping over.

That doesn't support your argument.  The point is that in the statement:

        foreach(@array) {
                ...
        }

@array should only be evaluated once, at the beginning of the loop.  In
effect (using := here, but otherwise Perl 5 code):

        my @arraycopy;
        for(my $i=0; $i < @array; $i++) {
                $arraycopy[$i] := $array[$i];
        }
        foreach(@arraycopy) {
                ...
        }

except without the actual overhead of having an @arraycopy.

--Brent Dax
[EMAIL PROTECTED]
Configure pumpking for Perl 6

When I take action, I'm not going to fire a $2 million missile at a $10
empty tent and hit a camel in the butt.
    --Dubya

Reply via email to