Today I ran into a little oddness with hash slices and using qw (grinder wrote an article on other issues here). It comes down to which quoting symbol you use:
use strict; use warnings; my %hash=(a=>1,b=>2,c=>4,d=>8); local $,="\t"; local $\="\n"; print @hash{qw'a b c d'}; # This throws a warning! print @hash{qw(a b c d)}; # This doesnt! __END__ Scalar value @hash{qw'a b c d'} better written as $hash{qw'a b c d'} at D:\temp\hashbug.pl line 8. 1 2 4 8 1 2 4 8
So if you ever run into this warning then just change the quoting character for qw and the error message goes away.

But still if theres a logical reason for this Id love to hear it.

UPDATE:
To make it even weirder it seems that when you use the following notation you dont get warnings

$ref=\%hash; print @{$ref}{qw'a b c'};

Yves / DeMerphq
--
Have you registered your Name Space?

Replies are listed 'Best First'.
Re: Bug with qw in hash slice.
by japhy (Canon) on Nov 22, 2001 at 20:38 UTC
    On second thought, it's hopeless. It uses simple parsing to determine if it should raise a warning. This code doesn't raise a warning:
    print @array[ 2];
    whereas this code does:
    print @array[2];
    I'd say, bugger all, use a different quoting character, or add a newline.
    Whoa, update. Since when does typing <code>...</code> without any newlines write inlined code? That seems new.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Indeed. As a bit of follow-up info to japhy's post: perl will issue the slice warning whenever what's inside a slice subscript (hash or array) matches the character class: [\w \t"'$#+-] from start to finish. Thus, we get warnings when the qw() operator uses any of: " ' $ + - # as delimiters --- with the exception of when something not in the character class above is inside the qw() operator (like qw'b *'). This also means that using such characters results in no warnings given even when one should be emitted (as in japhy's example of including a newline in the subscript). Here are a few more examples with hash slices:

      %h = (a => 1, b => 2, "*" => 3, 4 => '4a', ab => 42); @a = (1,2,3,4); $_ = 'aaaa'; print qq|@h{s+a+a+g}\n|; # 4a: warnings print qq|@h{s/a/a/g}\n|; # 4a: no warnings print qq|@h{qw'a b'}\n|; # 1 2: warnings print qq|@h{qw"a b"}\n|; # 1 2: warnings print qq|@h{qw+a b+}\n|; # 1 2: warnings print qq|@h{qw(a b)}\n|; # 1 2: no warnings print qq|@h{qw/a b/}\n|; # 1 2: no warnings print qq|@h{qw'b *'}\n|; # 2 3: no warnings print qq|@h{2 + 2}\n|; # 4a: warnings print qq|@h{3 + 5 - 4}\n|; # 4a: warnings print qq|@h{2 * 2}\n|; # 4a: no warnings print qq|@h{(3 + 5) - 4}\n|; # 4a: no warnings print qq|@h{'a'}\n|; # 1: warnings print qq|@h{'*'}\n|; # 3: no warnings print qq|@h{qq*a*}\n|; # 1: no warnings print qq|@h{'a'.'b'}\n|; # 42: no warnings print qq|@h{$a[3]}\n|; # 4a: no warnings print qq|@h{scalar @a}\n|; # 4a: no warnings

      The simple parsing scheme used is doomed both to issue inappropriate warnings, and neglect to issue appropriate warnings. Given that this is a parse/compile time warning and there can be arbitrary expressions in the subscript, getting it right would be very difficult at best (perhaps impossible).

      Typing <code>...</code> has always written inline code. In any case, I've seen this behaviours for months and months, so it's not new...

      --
      g r i n d e r
Re: Bug with qw in hash slice.
by japhy (Canon) on Nov 22, 2001 at 20:34 UTC
    The reason is because qw() and qx() is the only quoting operators that return a list. Perl disregards that, though, and just notices your use of a single OR double quote. The problem is in toke.c. I'm working on a fix right now, if I can.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;