Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Hard syntax error or disambiguable parsing?

by BrowserUk (Patriarch)
on Jan 29, 2009 at 03:28 UTC ( [id://739754]=note: print w/replies, xml ) Need Help??


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

(which you've written there misspelled as "for")

Rubbish! (Update: To quote you verbatim: "The words for and foreach have always been interchangable back to Perl version 0." )

absolutely wants to create a new local scalar variable

And yet, the first loop is legal.

Why bother replying?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^2: Hard syntax error or disambiguable parsing?

Replies are listed 'Best First'.
Re^3: Hard syntax error or disambiguable parsing?
by merlyn (Sage) on Jan 29, 2009 at 05:08 UTC
    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.

      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.

        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.
      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://739754]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found