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

The expression print qw/foo bar/ x 5 results in a syntax error. Here's the exact messages I get:

[am]king ~/a$ perl -wle 'print qw/foo bar/ x 5;' Unquoted string "x" may clash with future reserved word at -e line 1. Number found where operator expected at -e line 1, near "x 5" (Do you need to predeclare x?) syntax error at -e line 1, near "qw/foo bar/ x " Execution of -e aborted due to compilation errors. 255[am]king ~/a$ perl -v This is perl, v5.8.5 built for i686-linux Copyright...

I'm not sure what semantics this expression should have. It could be list repetition, like print((qw/foo bar/) x 5), or string repetition like print(scalar((qw/foo bar/) x 5)). I could imagine either: string repetition because the lhs of the x operator is not explicitly parenthisezed, or list repetition because perl treats qw as a parenthisized expression in print(qw/foo bar/[1]), and it is in list context here. However, I wouldn't have expected the current behaiviour: a (compile-time) syntax error.

So, all-knowing monks, I ask you to please explain me why this is a syntax error.

Update: I fixed a typo noticed by kutsu and others, where I had perl instead of print at one place.

Update: print qw/3 7/ - 5; is a syntax error too.

Update: ysth has kindly informed me in the CB that this has changed in bleadperl, and referred me to this perlbug discussion on it.

Replies are listed 'Best First'.
Re: repetition of qw: syntax error
by ysth (Canon) on Jun 22, 2005 at 21:35 UTC
    I believe it should be string repetition, but the powers that be have decided otherwise, and it will do list repetition in the future.
Re: repetition of qw: syntax error
by QM (Parson) on Jun 22, 2005 at 21:29 UTC
    perl -wle 'perl qw/foo bar/ x 5'

    Because perl is not a recognized function in Perl?

    Update: What you probably want is

    (qw/foo bar/) x 5

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: repetition of qw: syntax error
by graff (Chancellor) on Jun 23, 2005 at 02:13 UTC
    I'm guessing that the reason this sort of construct is not directly supported in "non-blead" perl is that, as a syntactic form, it has an inherent sort of ambiguity about it: should  qw/foo bar/ x 5 produce qw/foo foo ... bar bar .../, or qw/foo bar foo bar .../ (or "foo foo ... bar bar ..." or "foofoo...barbar...", etc)?

    It would be the kind of syntax where nearly everyone who chose to use it would end up having to read the man page every time, to make sure it really does what they want, and those who see it in someone else's code need to look it up to see what it really does.

    Personally, I would be content to use a more mundane -- but non-ambiguous -- approach like:

    my @base = qw/foo bar/; my @redup; push @redup, @base for ( 1..5 ); # qw/foo bar foo bar .../ # or for ( @base ) { push @redup, $_ for (1..5) } # qw/foo foo ... bar bar +.../ # or my $redup = join " ", map { "@base" } (1..5) # "foo bar foo bar ..." # etc.
      I'm guessing that the reason this sort of construct is not directly supported in "non-blead" perl is that, as a syntactic form, it has an inherent sort of ambiguity about it
      No, it was a blatant bug that made it not work (along with other things such as qw/1 2/ * 3). I'm pretty sure the recent fix will be included in 5.8.8 in due time.