Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: hash dereferencing issue with perl 5.16.3

by Athanasius (Archbishop)
on May 02, 2015 at 16:03 UTC ( [id://1125450]=note: print w/replies, xml ) Need Help??


in reply to hash dereferencing issue with perl 5.16.3

Hello KANAKADANDI, and welcome to the Monastery!

Consider the following excerpt from the Camel Book (4th Edition, 2012, p. 85):

When you evaluate a hash variable in scalar context, it returns a true value only if the hash contains any key/value pairs whatsoever. If there are any key/value pairs at all, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash.

So your error message shows that within the expression ${%{$lHashRef}}{'a'} the sub-expression %{$lHashRef} is being evaluated in scalar context. Like toolic, I looked at index-history but didn’t find anything to account for the change.

In more modern Perls, you have the following syntactic options for dereferencing the hash reference:

my $lValue = ${$lHashRef}{'a'}; my $lValue = $$lHashRef{'a'}; my $lValue = $lHashRef->{'a'};

The arrow (->) syntax is usually the best choice as it requires fewer sigils and makes it visually obvious that the expression on the left of the arrow is a reference.

...is there anyway to make the below script to work by changing some perl generic settings rather than changing the scripts??

Probably not. In any case, changing the syntax of the affected expressions from ${%{$ref}}{key} to $ref->{key} is likely the quickest fix, and will have the by-product of making the code more readable.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: hash dereferencing issue with perl 5.16.3
by hippo (Bishop) on May 02, 2015 at 17:03 UTC
    In more modern Perls, you have the following syntactic options for dereferencing the hash reference:
    my $lValue = ${$lHashRef}{'a'}; my $lValue = $$lHashRef{'a'}; my $lValue = $lHashRef->{'a'};

    I never use the first of these so cannot comment on that. However, my recollection of the second and third options is that both were available to use last millennium and probably in any version of Perl5. I'm not sure that these are best described as "more modern" these days and think it would be fair to say that use of this syntax would be perfectly valid in any perl likely to be encountered in production today.

    All this is just for clarity of course and I fully endorse and agree with the rest of your post.

      ${$lHashRef}{'a'} is just the "canonical" version of  $$lHashRef{'a'}. In the former syntax,  $lHashRef can be replaced by any expression that evaluates to a hash reference. In the latter syntax, thorny issues of precedence arise if  $lHashRef is replaced by anything other than another scalar hash reference.

      c:\@Work\Perl\monks>perl -wMstrict -le "my $s = undef; my $t = 0; my $u = { qw(a aye b bee) }; ;; print ${ $s || $t || $u }{a}; " aye


      Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1125450]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found