Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: Hard syntax error or disambiguable parsing?

by GrandFather (Saint)
on Jan 29, 2009 at 06:02 UTC ( [id://739779]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Hard syntax error or disambiguable parsing?
in thread Hard syntax error or disambiguable parsing?

The "variable" in a Perl for loop is aliased to each element that is being iterated over. It is effectively a symbolic place holder that is only valid within the scope of the loop body. However strict requires that variables are declared. Declaring the variable before the loop header makes it look like a normal variable - it ain't.


Perl's payment curve coincides with its learning curve.
  • Comment on Re^5: Hard syntax error or disambiguable parsing?

Replies are listed 'Best First'.
Re^6: Hard syntax error or disambiguable parsing?
by shmem (Chancellor) on Jan 29, 2009 at 10:15 UTC
    ... a symbolic place holder that is only valid within the scope of the loop body.

    Right, but it is subject to the same scoping rules as any other perl variable, i.e. to the variable it aliased:

    use strict; our $i; sub bar { print "<$i>" } for $i (1..3) { bar } # localized for my $i (1..3) { bar } print "'$i'" __END__ <1><2><3><><><>''
    use strict; my $i; sub bar { print "<$i>" } for $i (1..3) { bar } # already 'my' for my $i (1..3) { bar } print "'$i'" __END__ <><><><><><>''
Re^6: Hard syntax error or disambiguable parsing?
by ack (Deacon) on Jan 29, 2009 at 16:17 UTC

    AhHa! In my earlier comment on one of the other threads, I had forgotten that the loop variable is not only lexical and in-scope in the loop's block, but that it is also aliased to the iterator.

    Thanks GrandFather. I learn so much (sometimes mulitiple times since I am frequently not a very memory-rich Pilgrim) from the Monestary.

    I am curious, however. Since the loop variable is aliased to the iterator (or to the iterator's values?); how does that work when the iterator doesn't appear to have variables, but rather constants (as in the case this thread is addressing (i.e., the (0..5) iterator)? And what would happen if you tried to change the iterator loop variable within the loop block>

    By way of an example of that last question:

    foreach $i (0..5){ $i = 9; print "$i\n"; }

    Actually I can answer that myself by trying it. No need to answer that part of the question; I don't want to abuse the Monks with things I can easily do my self.

    But the question before that last one is still nagging me.

    ack Albuquerque, NM
Re^6: Hard syntax error or disambiguable parsing?
by pobocks (Chaplain) on Jan 29, 2009 at 06:48 UTC

    Danke schonn for this and your other reply.

    I think I have too much of a tendency to try and explain away magic as a result of regular syntax objects; sometimes, there's just a wizard behind that curtain ;-)

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      Well, a lot of the magic does just come down to pulling the right levers. ;)


      Perl's payment curve coincides with its learning curve.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found