in reply to Re^12: eof not recognised when applying diamond operator to invocation arguments?
in thread eof not recognised when applying diamond operator to invocation arguments?

Good point about the syntax, though the end result is the same -- "for" and "foreach" are given separate treatment.

Regarding Perl style "for" loops, I think in general, if I was using the "(;;)" format then I would use "for" and if I was using the "$var (list)" format I would use foreach. But there could be exceptions and I'm not sure how to articulate it any better than in my first attempt. (^_^;) :-/

Regarding the missing text, this is what I wrote:

My point about discouraging "for" loops was if the two were actually two distinct looping styles (i.e. that "foreach" was not a synonym for "for", so that "for" could only be used for C-style for loops). Sorry if there was any confusion over that.
If you see all of that then what I meant was "my comment above about discouraging the use of 'for' loops was predicated on restricting "for" loops to only the C-style (;;) syntax".

Elda Taluta; Sarks Sark; Ark Arks

  • Comment on Re^13: eof not recognised when applying diamond operator to invocation arguments?

Replies are listed 'Best First'.
Re^14: eof not recognised when applying diamond operator to invocation arguments?
by ikegami (Patriarch) on Jan 18, 2011 at 19:40 UTC

    My question is still unanswered. I'm trying to determine which of the following you would use and why:

    for (1..5) { say; } foreach (1..5) { say; }

    I've been specifically asking how the two argument form fits in, yet you keep answering with statements that only cover the three argument form and the list argument form.

      The optimization which makes for (1..5) {...} special is supposed to be invisible to Perl programmers. (Other than the reduced memory usage...) So if someone formatted the list form differently than the three argument form, I would expect them to format that in the list form, which is what it acts like.

        I'm asking what he uses and what are his reasons, but that may very well be his answer. If he had answered what you answered, I would have pointed out three things.

        First, I would have pointed out that his answer conflicts with what he's said so far. He said his choice of keyword is based on the section in which the form is documented, yet there is no documentation that covers this form. (The docs for foreach loops claim to talk about loops that create "a list value".)

        Secondly, I would have pointed out that I find very unfortunate that some people pretend that Perl counting loops are foreach loops. Worse yet, they pretend there's some self-evident reasoning that it should be that way. It fools people into wanting to use C-style loop in some attempt to grab performance. (When I thought for (X..Y) was a foreach loop, I didn't use it.) It also fools people into making style arguments based on the incorrect proposition that Perl only has two for loop implementations.

        Finally, I would have pointed that I find silly that the driving force behind the choice syntax is to differentiate nothing more than the syntax, especially when it differs from function and efficiency. It seems to me that everyone eventually realises that the redundant syntax clue is completely useless and that they've been forcing themselves to type more than required for nothing.

        which is what it acts like.

        It doesn't act like a foreach loop. Many people avoid it because they think it does.