qw() in list context deprecated

Someone decided that using `for var qw()` should be forbidden, you have to write explicitly now `for ... (qw())` instead of `qw()` beginning with 5.14. WTF

5.14 seemed to be a fine release for me, the first in a long time which is actually faster then most of the previous ones. And there were not too many languages policists.
But this new qw() deprecation warning is just pure nonsense.

`qw` used in for list where only list-context can be used should still be allowed.
Why should I be forced to update all my code to add `()` around `qw()` only because it seems to be "right". perl has let you use handy shortcuts forever.

for my $a qw() {} => for my $a (qw()) {}
How awful!
$ perl5.13.5 -e 'for my $a qw(1 2) {print $a;}'

Use of qw(...) as parentheses is deprecated at -e line 1.
12

$ perl5.13.4 -e 'for my $a qw(1 2) {print $a;}'
12

$ perl5.13.4 -e 'require B; B->import qw(main_root)'

$ perl5.13.5 -e 'require B; B->import qw(main_root)'
Use of qw(...) as parentheses is deprecated at -e line 1.


There is no other context possible for the `qw` here than list context,
so perl should be consistent with other list grabbing functions like print
and don't enforce quotes were they are not needed.

This is certainly not DWIM, this is language police.

The language police should rather think of getting rid of more unneeded syntax, than enforcing it.

`for my $var` always should eat the next tokens as list members until the block. Parens are really not needed here.

$ perl -e 'for my $a 1,2 {print $a;}' => syntax error

There's nothing else possible than a list context after the for variable.

And why is this still not possible?

$ perl -e 'for 1,2 {print $_}' => syntax error

11 Comments

Hmms, I really hope that (qw(a b c)) stuff is an April 1st joke. PLEASE!

Totally agree with you, Reini. Please, do not add syntax sugar where it is not needed.

Hmms, can we have a 'use deprecated;' pragma? hehehe!

Someone decided that using qw() in list context should be forbidden,

Who? Can you point to something referring to this? I'd love to see the rationale.

Here it is. This is a cleanup of the parser. E.g. you can't do for my $i q[foo] { say $i }, but you can do for my $i qw[foo] { say $i }. But the latter now warns, and will be deprecated eventually.

I think the title of this blogpost is greatly misleading. In 99% of it's uses, qw// can still be used in places where it is currently used. In fact your examples are pretty much the only ones where they can't.

In the old parser, qw// allowed you to omit otherwise mandatory parentheses in a very select set of circumstances. For-statements (but only when they have an explicitly named iterator) and methods calls are such cases. In both of these circumstances the parentheses have very specific meanings that are different from being a list. The old behavior should never have been allowed in the first place.

This change is for good reasons (the way the lexer currently implements qw is quite insane, inserting tokens that aren't really there). For a detailed explanation see this thread.

you have to write explicitly now `(qw())` instead of `qw()` beginning with 5.14.

Entirely untrue. qw() still behaves the same in all contexts except for one, which is

for $_ qw(foo bar) { ... }

That is, in situations where perl's syntax normally requires parens, but qw() could be used to omit them. This being allowed was simply an odd side-effect of how the parser was written, and has now been fixed.

Used to work, is being deprecated:

for my $a qw( 1 2 ) { print $a }

Has never worked:

for qw( 1 2 ) { print }

Will explicitly continue to be supported:

my @arr = qw( 1 2 );

Practically nothing except the first example is changing.

I never actually knew you could do this:


for my $i qw[foo] { say $i }

If it simplifies the core and is (as it seems) the change I say thank you and good riddance ;)

Everything old is new again. Permitting "for qw(foo bar) { ... }" was new in 5.8, as I recall. Prior to that, you *had* to write "for (qw(foo bar)) { ... }". I think we're just heading back there. But why?

And that's the weird part. In 5.6 or so, you *had* to have these parens. It's just returning to the old syntax. Oh well!

About Reini Urban

user-pic Working at cPanel on cperl, B::C (the perl-compiler), parrot, B::Generate, cygwin perl and more guts, keeping the system alive.