Warning scalar value @h{...} better written as $h{...}

Discussion in 'Perl Misc' started by Wolf Behrenhoff, Aug 10, 2011.

  1. Hello all,

    why does the following code produce a warning?

    perl -wle'opendir $d, "/home";my %h;@h{readdir $d}=();print for keys %h'
    Scalar value @h{readdir $d} better written as $h{readdir $d} at -e line 1.

    Of course, one must not follow the advice because that would do
    something different.

    One can of course write
    @h{@{[readdir $d]}}=();
    to suppress the warning, but that looks a bit complicated.

    Tested with v5.8.8 and v5.10.1.

    Wolf
    Wolf Behrenhoff, Aug 10, 2011
    #1
    1. Advertising

  2. Wolf Behrenhoff <> writes:
    > perl -wle'opendir $d, "/home";my %h;@h{readdir $d}=();print for keys %h'
    > Scalar value @h{readdir $d} better written as $h{readdir $d} at -e
    > line 1.


    My guess (I haven't checked the code) would be that it wrongly
    considers 'readdir $d' to be a string which will be autoquoted when
    used as a hash subscript and because of this, thinks you are using a
    slice where accessing a single element would be sufficient.

    [...]

    > One can of course write
    > @h{@{[readdir $d]}}=();
    > to suppress the warning, but that looks a bit complicated.


    Another workaround is to make 'readdir' look like a function call,
    that is, use readdir($d), at least for 5.10.1.
    Rainer Weikusat, Aug 10, 2011
    #2
    1. Advertising

  3. Rainer Weikusat <> writes:
    > Wolf Behrenhoff <> writes:
    >> perl -wle'opendir $d, "/home";my %h;@h{readdir $d}=();print for keys %h'
    >> Scalar value @h{readdir $d} better written as $h{readdir $d} at -e
    >> line 1.

    >
    > My guess (I haven't checked the code) would be that it wrongly
    > considers 'readdir $d' to be a string which will be autoquoted when
    > used as a hash subscript


    I just had a look at the code because I was curious and the following
    check is used in order to determine if the operation inside the
    slice-defining subscript cannot possibly yield more than one value:

    if (*s == '[' || *s == '{') {
    if (ckWARN(WARN_SYNTAX)) {
    const char *t = s + 1;
    while (*t && (isALNUM_lazy_if(t,UTF) || strchr(" \t$#+-'\"", *t)))
    t++;
    if (*t == '}' || *t == ']') {
    t++;
    PL_bufptr = PEEKSPACE(PL_bufptr); /* XXX can realloc */
    Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
    "Scalar value %.*s better written as $%.*s",
    (int)(t-PL_bufptr), PL_bufptr,
    (int)(t-PL_bufptr-1), PL_bufptr+1);
    }
    }
    }

    This is, of course, just plain broken:

    [rw@sapphire]~ $perl -we 'my @a; @a[1 + 1] = 2;'
    Scalar value @a[1 + 1] better written as $a[1 + 1] at -e line 1.
    [rw@sapphire]~ $perl -we 'my @a; @a[(1 + 1)] = 2;'
    [rw@sapphire]~ $
    Rainer Weikusat, Aug 10, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    547
    reaway lee
    Aug 24, 2003
  2. helmut januschka

    warning: excess elements in scalar initializer

    helmut januschka, Mar 6, 2004, in forum: C Programming
    Replies:
    5
    Views:
    15,145
    Joe Wright
    Mar 6, 2004
  3. KaiWen
    Replies:
    102
    Views:
    2,654
    Jorgen Grahn
    Sep 15, 2011
  4. Clint Olsen
    Replies:
    6
    Views:
    341
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    154
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page