Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Hard syntax error or disambiguable parsing?

by merlyn (Sage)
on Jan 29, 2009 at 05:08 UTC ( [id://739764]=note: print w/replies, xml ) Need Help??


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

Sure, the words are interchangable, in the same way that you can omit $_ a lot. It's not as clear, and this is clearly a beginner, so I thought I'd bring clarity back.

As for the second statement, I don't understand your complaint. The first loop does indeed create a new local scalar variable, as I said.

Replies are listed 'Best First'.
Re^4: Hard syntax error or disambiguable parsing?
by pobocks (Chaplain) on Jan 29, 2009 at 05:38 UTC

    It doesn't reuse the one created in the above line my $i;? Or do you mean uninitialized, rather than previously undeclared. Either way, why does a scalar member of an array not qualify?

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      my $i = 5; for $i (10..15) { } print "$i\n";
      prints 5. So clearly, the $i being iterated is not the $i from the my immediately above it.

      A scalar member of an array is not a simple scalar. By Larry's rule, it has to be a simple scalar.

        Ahhhh... and he was enlightened. Basically, $i here is operated on as if it was:

        my $i; foreach local $i (0 .. 5){ ... }

        Do you have any ideas/pointers to the rationale for "no array members?" I understand that it is so, but fail to understand why it is so, when (for instance) one can local-ize individual array members.

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

        I just tried the following code...which is a variation on what you show.

        #!/user/bin/perl use warnings; use strict; my $i = 10; print "$i\n"; foreach $i (0..5){ print "$i\n"; } # end foreach $i loop print "$i\n"; exit(0);

        It prints the following:

        10 0 1 2 3 4 5 10

        This is exactly what I believe the Camel says it should do since the loop variable is, if I recall correctly, always created anew as a lexical whose scope is the subsequent loop block (even though it appears to be created before the loop block). The output above seems to confirm that.

        I, like BrowserUk, however am a bit perplexed by the inability to use the $i[0] construct as the loop variable.

        Is it because the $i[0] implies a list structure which does not get created (via, for example, autovivification...spelling?)?

        ack Albuquerque, NM
        How could I get the for loop to use the predeclared $i instead of using a local one?

      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.
        ... 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__ <><><><><><>''

        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

        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";
Re^4: Hard syntax error or disambiguable parsing?
by Marshall (Canon) on Jan 30, 2009 at 19:21 UTC
    I for one applaud your statement about clarity! I see a lot of code on Monks that uses obscure features of Perl when they aren't necessary.

    I also see a massive fascination with $#list. I don't understand why that is! The scalar value of @list does everything I need.

    One of the magic things about Perl is the ability to iterate over a list without knowing or caring about the "last index" or how many things are even in the list!

    I always use foreach(@list){} instead of for(@list){}. A "C" style "for loop" is a rare duck in Perl (although seldom doesn't mean never). Even though "for" and "foreach" are equivalent in this case, foreach my $variable (@list){} is more clear. HORRAY!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-03-29 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found