Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Warning scalar value @h{...} better written as $h{...}

Reply
Thread Tools

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

 
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      08-10-2011
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
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-10-2011
Wolf Behrenhoff <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-10-2011
Rainer Weikusat <(E-Mail Removed)> writes:
> Wolf Behrenhoff <(E-Mail Removed)> 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]~ $
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generally, are the programs written by C++ slower than written by C10% ? KaiWen C++ 102 09-14-2011 11:12 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM
Re: Can a usercontrol written in C# be used in Web Forms that is written in VB.Net? Steve C. Orr, MCSD ASP .Net 1 08-24-2003 12:06 AM



Advertisments