Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Variable scoping, foreach loop, alias

by Vaati (Sexton)
on Jan 23, 2004 at 21:06 UTC ( [id://323691]=perlquestion: print w/replies, xml ) Need Help??

Vaati has asked for the wisdom of the Perl Monks concerning the following question:

I'm not exactly new to perl and I'm not exactly a guru but for the life of me I can't figure out why this does what it does.
use strict; my @arry = (1,2,3,4,5 ); foreach my $bla ( @arry ) { $bla++; } foreach ( @arry ) { print "$_\n"; }
Everything I've ever seen/done/read in perl has not
clarified why this thing prints
2
3
4
5
6

??? Things like this just make you feel dumb.
Oh there is one other thing, this script is running under
the nebulous environ of mod_perl if that makes a diff.

edit by thelenm: added code tags

janitored by ybiC: Retitle from less-than-descriptive "Okay, I'm needing some clarification."

Replies are listed 'Best First'.
Re: Variable scoping, foreach loop, alias
by Zaxo (Archbishop) on Jan 23, 2004 at 21:11 UTC

    Your $bla is not an ordinary variable in the foreach loop. foreach my $bla (@arry) {} makes $bla an alias to each element of @arry in turn. The increment acts on the original array elements.

    After Compline,
    Zaxo

Re: Variable scoping, foreach loop, alias
by thelenm (Vicar) on Jan 23, 2004 at 21:11 UTC

    In your loop, $bla is being aliased to each element of the array in turn. So, when you modify $bla, you are really modifying the array element that $bla is referring to.

    The same thing happens with $_ if you don't specify a variable. For example, try this as well:

    my @arry = (1, 2, 3, 4); ++$_ for @arry; print "$_\n" for @arry;

    -- Mike

    --
    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

      I didn't realize that it only became an alias. This explains a lot to me. I appreciate your two replies!! Thanx a lot zaxo and thelenm for the insight!

      Paulster2

Re: Variable scoping, foreach loop, alias
by Fletch (Bishop) on Jan 23, 2004 at 21:25 UTC

    Read more, specifically perldoc perlsyn:

    If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element if 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.
Re: Variable scoping, foreach loop, alias
by Roy Johnson (Monsignor) on Jan 23, 2004 at 21:13 UTC
    In a word, aliasing. Foreach loop variables are aliased to the elements they are iterating over. Modifying the loop variable modifies the original element.

    If you hard-code your list into the loop, instead of using an array, you'll get an error when it tries to modify it.


    The PerlMonk tr/// Advocate

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://323691]
Approved by xenchu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found