Some years, months ago I found here on PerlMonks that parentheses are not needed around qw// to create a list for for consumption. Today, I found a failure case listed after working versions ...

#!/usr/local/bin/perl use strict; use warnings; # Working versions. warn $_ for qw/ p / ; for my $var qw/ p / { warn $var } for ( qw/ p / ) { warn $_ } # Fails at compile time. # This is commented just to have the program be compiled and get erro +r via # eval. #for qw/ p / { warn $_ }; my $code = eval "for qw/ p / { warn $_ } " ; warn "code: $code error: $@"; __END__ Error message produced ... Missing $ on loop variable at -e line 1 (#1) ... diagnostics module adds ... (F) Apparently you've been programming in csh too much. Variables + are always mentioned with the $ in Perl, unlike in the shells, whe +re it can vary from one line to the next.

... seems the failing loop is being parsed as for p ( ... ) { ... }.

Perl versions tested were 5.8.7 i686-linux-thread-multi & 5.10.0 i686-linux.

Any comments on if the parentheses lacking versions work by fluke? Or, if the failure is by fluke?

Belatedly added the link to support my fuzzy memory, removed comp.lang.perl.misc reference. Other related discussions ...

Replies are listed 'Best First'.
Re: "for qw/ word / { warn $_ }" fails, other parentheses lacking versions work
by grinder (Bishop) on Apr 04, 2008 at 08:38 UTC

    The block modifier (for at the end of a statement) is a different kettle of fish and not subject to the same rules.

    Other than that, yes, this ambiguity was discussed on perl5-porters a couple of years ago, but try as I might, I can't coax the thread out into the open.

    The details as I remember them was that this syntax (a qw list without parens) happened to work by accident, and in correcting a problem elsewhere in the parser, the syntax began to fail. As it was never explicitly authorised by the language, it was deemed easier to ban the syntax rather than torture the tokeniser even more, in order to allow it to continue to accept this syntax.

    • another intruder with the mooring in the heart of the Perl

      The "Surpised by foreach iterator limitation" (2003) subthread seems to give the impression that a for loop working with parentheses lacking qw// was intentional.

      In the meantime, I will try to find the thread that you speak of.