Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

"Using a hash as a reference is deprecated" -- Baloney !

by Anonymous Monk
on May 11, 2011 at 20:07 UTC ( #904238=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Wait a minute --- what's wrong with these lines ?! These look perfectly fine!

$WAI is an object, which I call getMem() which returns a hashref, I then address a piece of the hashref with ->{'MemTotal'} and take that inside the object's converter function kBToMB().

Is this bad ?? What's a better way to achieve the same thing?

----------------------------

sub get_memory_tot { return $WAI->kBToMB( %{$WAI->getMem()}->{'MemTotal'} ); } sub get_memory_free { return $WAI->kBToMB( %{$WAI->getMem()}->{'MemFree'} ); } sub get_swap { return $WAI->kBToMB( %{ $WAI->getMem() }->{'SwapTotal'}); }

Replies are listed 'Best First'.
Re: "Using a hash as a reference is deprecated" -- Baloney !
by kennethk (Abbot) on May 11, 2011 at 20:15 UTC
    If getMem() is returning a hash reference, then you are dereferencing the hashref into a hash with %{ ... } and then dereferencing a second time using The Arrow Operator, thus the warning. The preferred syntax would be:

    sub get_memory_tot { return $WAI->kBToMB($WAI->getMem()->{'MemTotal'} ); } sub get_memory_free { return $WAI->kBToMB( $WAI->getMem()->{'MemFree'} ); } sub get_swap { return $WAI->kBToMB( $WAI->getMem()->{'SwapTotal'}); }

    See perlref and/or perlreftut for more explanation.

Re: "Using a hash as a reference is deprecated" -- Baloney !
by NetWallah (Canon) on May 11, 2011 at 20:17 UTC
    Paraphrasing from from perlop:The left side of the "->" ( infix dereference operator) must be a REFERENCE to ....

    In your case, you are supplying a HASH (Not a reference).

    Just get rid of the %{...} , and you will have a (Hash) reference.

    return $WAI->kBToMB( $WAI->getMem()->{MemTotal} );
    (No need for quotes inside {} ).

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: "Using a hash as a reference is deprecated" -- Baloney !
by ikegami (Pope) on May 11, 2011 at 20:24 UTC

    Due to a bug in Perl,

    %foo->{bar}

    gets interpreted as a

    (\%foo)->{bar}

    ->{bar} expects a reference, and thus a scalar, so it should be interpreted as

    scalar(%foo)->{bar}

    The deprecation warning was added in anticipation of getting this bug fixed.

    Since

    %{$WAI->getMem()}->{'MemTotal'}

    is getting interpreted as

    \%{$WAI->getMem()}->{'MemTotal'}

    you could use that, or you could shorten it to

    $WAI->getMem()->{'MemTotal'}

    Now where's this baloney you promised? I'm hungry!

    Update: Fixed bad curlies matching in second half.

      Awesome. Excellent explanation, I had no idea this was a planned bugfix. Thanks everyone ! Here's the promised "baloney" :)
      Balogna ("baloney") and cheese: _.---._ _.-~ ~-._ _.-~ ~-._ _.-~ ~---._ _.-~ ~\ .-~ _.; :-._ _.-~./ }-._~-._ _..__.-~_.-~ ) `-._~-._~-._ / ...-~H.-~ ~-nad.._\. _.-~ .:::: // ~-. \`--...--~ _.-~__...==~ \.`--...---+-~~~~~ ~-..----~ How about a deli style turkey sandwich (damn I'm hungry now!): _.---._ _.-~ ~-._ _.-~ ~-._ _.-~ ~---._ _.-~ ~\ .-~ _.; :-._ _.-~ ./ `-._~-._ _..__.-~ _.-~ / ~-._~-._ / .__..--~----._ \_____(_;-._\. _.-~_/ ~).. . \ /(_____ \`--...--~_.-~______..-+_______) .(_________/`--...--~/ _/nad /\ /-._ \_ (___./_..-~__.....__..-~./ `-._~-._ ~\--------~ .-~_..__.-~ _.-~ ~-._~-._ ~---------' / .__..--~ ~-._\. _.-~_/ \`--...--~_.-~ `--...--~ Gotta have something to wash that down with... _ // // _______________//__ .(______________//___). | / | |. . . . . . . / . . .| \ . . . . . ./. . . . / | / ___ | _.---._ |::......./../...\.:| _.-~ ~-._ |::::/::\::/:\::::::| _.-~ ~-._ |::::\::/::::::X:/::| _.-~ ~---.;:::::::/::\::/:::::| _.-~ ~\::::::n::::::::::| .-~ _.;::/::::a::::::::/ :-._ _.-~ ./::::::::d:::::::| `-._~-._ _..__.-~ _.-~|::/::::::::::::::| / ~-._~-._ / .__..--~----.YWWWWWWWWWWWWWWWP' \_____(_;-._\. _.-~_/ ~).. . \ /(_____ \`--...--~_.-~______..-+_______) .(_________/`--...--~/ _/ /\ /-._ \_ (___./_..-~__.....__..-~./ `-._~-._ ~\--------~ .-~_..__.-~ _.-~ ~-._~-._ ~---------' / .__..--~ ~-._\. _.-~_/ \`--...--~_.-~ `--...--~

      credit to : Nate / DAC for the artwork !

Re: "Using a hash as a reference is deprecated" -- Baloney !
by LanX (Sage) on May 11, 2011 at 20:23 UTC
    Well you are overcompensating! ;)

    Either $WAI->getMem()->{'SwapTotal'} or (which nobody mentioned yet)  %{ $WAI->getMem() }{'SwapTotal'} but not both.

    Old perl versions allowed to write %hash->{key} but as you see it's deprecated now.

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2021-11-30 18:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?