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

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

eh? Not only are the keywords interchangeable, but for $i ( 0 .. 5 ) is not a foreach loop as defined in perlsyn. It shares traits with both foreach loops (syntax) and for loops (purpose and efficiency).

It's not as clear, and this is clearly a beginner, so I thought I'd bring clarity back.

So how do you decide which keywords to use? Choosing the keyword based on syntax (looks like a list or not) is redundant. The only meaningful criteria I can think of are memory efficiency (you'd use for here) or purpose (you'd use for here).

Replies are listed 'Best First'.
Re^3: Hard syntax error or disambiguable parsing?
by BrowserUk (Patriarch) on Jan 29, 2009 at 08:24 UTC
    but for $i ( 0 .. 5 ) is not a foreach loop as defined in perlsyn.

    It's good to know that someone else around here actually reads the docs--and arrives at the same interpretation as me.

    Personally, the whole 'unavoidable implicit localisation' of for loop variables smacks of an accidental implementation bug that got post-facto ligitimisation via Marketeer Maintainenance: The re-branding of bugs as features.

    There doesn't seem to be any logical reason why an existing variable shouldn't be used (unlocalised) as the loop iterator, given that if a strictly loop scoped iterator is required, the inline-my version achieves that perfectly.

    This would allow several common uses that currently require extra steps to achieve. For example, retaining the information of how far the loop iterated if it is terminated early via last, that currently requires an extra variable and another assignment.

    If this was an explicit decision--rather than an accidental happenstance as suggested by some of the comments in the source files--then it would be good to see the reasons laid out.


    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.
      Personally, the whole 'unavoidable implicit localisation' of for loop variables smacks of an accidental implementation bug that got post-facto ligitimisation via Marketeer Maintainenance: The re-branding of bugs as features.
      Indeed. The original designer of Perl ought to have repented long ago and designed a better language as penance. :-)

      When I look at this whole discussion, I see:

      Perl 5: A loop variable is defined as BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH except when BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH except nowadays you should really BLAH BLAH BLAH BLAH no it isn't yes it is no it isn't BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH...

      Perl 6: A loop variable is simply a formal parameter to a block.

        Indeed.

        When I posted the OP, I really was hoping for a 'use an anonymous block to disambiguate an indirect object' type of fix.

        The original designer of Perl ought to have repented long ago and designed a better language as penance. :-)

        Don't be too hard on him. I'm still of the opinion that he did a pretty damn good job with Perl 5.

        So good in fact that after 7 years of pretty in-depth usage I'm still 'discovering' built-in features that allow one or two statements to do the same job as would take one or two screenfuls in most other languages.

        What's more, the way the language elements combine to effect those features, they can not have come about by a accident. They had to have been thought about in depth at the design stage, despite the fact that in many cases there are one (or a dozen) modules on CPAN that re-implement those features, (often badly), using one or two (or three) screenfuls. Usually authored by someone who went looking for the feature but failed to recognise the forethought that had gone into providing for it, and simply missed it.

        As for Perl 6. Raduko seems to be coming along quite nicely. I really hope that the implementation eventually meets up with the authors vision. I can see myself spending twice as long (and more) as its prolonged gestation, exploring all it's facets. And thoroughly enjoying the experience.


        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.
Re^3: Hard syntax error or disambiguable parsing?
by rovf (Priest) on Jan 29, 2009 at 09:02 UTC
    for $i ( 0 .. 5 ) is not a foreach loop

    May I ask you in what way

    for $i ( 0 .. 5 )
    and
    foreach $i ( 0 .. 5 )
    differ, according to your opinion? perlsyn says The "foreach" keyword is actually a synonym for the "for" keyword, so I don't see how you came to the idea that the for loop your mention, would not be a foreach loop. Did I miss something here?

    -- 
    Ronald Fischer <ynnor@mm.st>

      Did I miss something here?

      You probably think the ".." in "for[each] $i ( 0 .. 5 )" is the range operator and that it creates a list of 6 elements. It's not, and no list of 6 elements is created.

      perlsyn distinguishes between For Loops ("C"-style For Loops) and Foreach Loops (List Iteration Loops), but "for[each] $i ( 0 .. 5 )" is neither. It's actually a Counting Loop like FOR ... NEXT in BASIC.