rovf has asked for the wisdom of the Perl Monks concerning the following question:

I had the following piece of code

foreach () {}
I was supposed that this would act as a no-op - looping over an empty list and doing nothing - and was only meant as a placeholder where I wanted to put in the "real meat" later. To my surprise, this didn't compile:

syntax error ... near "()"

Why does it reject my empty list? I re-read perlsyn, but didn't find anything which would forbid such a construct.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: foreach over empty list
by BrowserUk (Patriarch) on Jun 06, 2011 at 14:51 UTC

    The first set of parens are a required part of the for/foreach syntax, not an empty list. Put an empty list inside them and all is well:

    c:\test\xx>perl -we"for () { print; }" syntax error at -e line 1, near "() " Execution of -e aborted due to compilation errors. c:\test\xx>perl -we"for (()) { print; }"

    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.