#!/usr/bin/perl -w
use strict;
my @array;
### works
push @array, ('X') x 2;
print @array, "\n";
### doesn't work - x is bareword?
#push @array, qw(X) x 2;
#print @array, "\n";
### works
push @array, (qw(X)) x 2;
print @array, "\n";
### works
push @array, +(qw(X)) x 2;
print @array, "\n";
x doesn't play by normal rules and will do things slightly funny just because the qw() used parens instead of some other delimiter. So x's special handling are higher priority just because it's special. You shouldn't infer much from it. This is likely all very implementation specific too so it could change.
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
I get the same behaviour with other delimiters.
@array = qw!X! x 2; print @array, "\n";
gives
Unquoted string "x" may clash with future reserved word at 541179.pl line 3.
Number found where operator expected at 541179.pl line 3, near "x 2"
(Do you need to predeclare x?)
syntax error at 541179.pl line 3, near "qw!X! x "
Execution of 541179.pl aborted due to compilation errors.
You implied the problem is related to use strict, but it isn't:
@array = qw(X) x 2; print @array, "\n";
gives
Unquoted string "x" may clash with future reserved word at script.pl line 4.
Number found where operator expected at 541178.pl line 4, near "x 2"
(Do you need to predeclare x?)
syntax error at 541178.pl line 4, near "qw(X) x "
Execution of 541178.pl aborted due to compilation errors.
I've noticed this bug before.
#!/usr/bin/perl -w use strict; my $scalar = qw(A B C); print $scalar, "\n";Gives:
What version of perl are you using? Mine doesn't say that at all.
Quote split:
In scalar context, returns the number of fields found and splits into the @_ array. Use of split in scalar context is deprecated, however, because it clobbers your subroutine arguments.
Your version of Perl probably implements qw(A B C) as split('', q(A B C)). If you want the count, use my $scalar = () = qw(A B C);.
I say "your version", because perl 5.6.1 gives
Useless use of a constant in void context at 541192.pl line 4. Useless use of a constant in void context at 541192.pl line 4. C
$ perl -w use strict; my $scalar = qw(A B C); Useless use of a constant in void context at - line 3. Useless use of a constant in void context at - line 3. print $scalar, "\n"; C
$ perl -v This is perl, v5.8.7 built for cygwin-thread-multi-64int (with 1 registered patch, see perl -V for more detail)By the way, this has nothing to do with qw.
$ perl -w my $scalar = (1, 2, 3); Useless use of a constant in void context at - line 1.
qw/STRING/ Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters. It can be under- stood as being roughly equivalent to: split( , q/STRING/); the differences being that it generates a real list at compile time, and in scalar context it returns the last element in the list.
So why in the heck would you use this operator on one value in the middle of a push statement. The interpreter does its best but you are making it hemmorage here. use the q operator.
#This works: push @array, q(X) x 2; print @array, "\n";
#!/usr/bin/perl -w
use strict;
my @array;
push @array, q(X) x 2;
print join ' - ', @array, "\n";
@array = ();
push @array, ('X') x 2;
print join ' - ', @array, "\n";
$ perl587 -wle 'print for qw(X Y) x 2'
Unquoted string "x" may clash with future reserved word at -e line 1.
Number found where operator expected at -e line 1, near "x 2"
(Do you need to predeclare x?)
syntax error at -e line 1, near "qw(X Y) x "
Execution of -e aborted due to compilation errors.
$ perl588 -wle 'print for qw(X Y) x 2'
X
Y
X
Y
$
Dave.
Indeed! Quote perl588delta:
You can now use the x operator to repeat a qw// list. This used to raise a syntax error.
perlmonks.org content © perlmonks.org and codeacrobat, dave_the_m, diotalevi, doc_faustroll, duff, eff_i_g, ikegami
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03