I'm wondering if this relates to perl having difficulty distinguishing between the qw## operator versus a bare hash key that looks like qw##. I guess there's not much to wonder about there. That seems to be what's happening:

use 5.010_001; use strict; use warnings; my %hash = qw/ one um two dois /; { local $, = "\t"; say @hash{ qw# one two # }; say @hash{ (qw# one two #) }; }

Output is:

Scalar value @hash{ qw# one two # } better written as $hash{ qw# one t +wo # } at mytest.pl line 12. um dois um dois

Only one warning. The second hash slice uses parens to disambiguate the intent of whether qw## represents a hash key, or a qw## operator.

By the way, if I use diagnostics, I get:

Scalar value @hash{ qw# one two # } better written as $hash{ qw# one two # } at mytest.pl line 13 (#1) (W syntax) You've used a hash slice (indicated by @) to select a single element of a hash. Generally it's better to ask for a scalar value (indicated by $). The difference is that $foo{&bar} always behaves like a scalar, both when assigning to it and when evaluating its argument, while @foo{&bar} behaves like a list when you assign to it, and provides a list context to its subscript, which can do weird things if you're expecting only one subscript. On the other hand, if you were actually hoping to treat the hash element as a list, you need to look into how references work, because Perl will not magically convert between scalars and lists for you. See perlref.

...which seems to support that qr## is being seen by the warnings pragma as a hash key instead of a list constructor. On the other hand, perl still sees it as a list constructor, and does the right thing. I know just about zero with regards to Perl's internal code, but it would seem the culprit is within the warnings mechanism.

But I'm putting this out there for discussion's sake. I'm as curious as anyone and am just speculating reasons.


Dave


In reply to Re^2: Incorrect warning when using hash slices? by davido
in thread Incorrect warning when using hash slices? by flipper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.