Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Updating hash using it's reference ...

by AnomalousMonk (Archbishop)
on Jul 12, 2014 at 13:18 UTC ( [id://1093372]=note: print w/replies, xml ) Need Help??


in reply to Re: Updating hash using it's reference ...
in thread Updating hash using it's reference ...

That last bit of syntax is clunky ... the -> postfix operator for dereferencing and element access as well ...

And for even less clunky syntax, Perl understands that any array/hash/code/etc. reference below the topmost level can only be a reference, so it will use the  -> operator implicitly if you do not use it explicitly. It's only at the topmost level that you have to supply a  -> or not based on the nature of the variable you are accessing:  -> must be used with an array/hash/code reference: see Anonymonk's reply for an example of this.

c:\@Work\Perl>perl -wMstrict -le "use feature qw/say/; use Data::Dump; ;; my @array = ( { 'Fred' => 'Flintstone', 'Barney' => 'Rubble', } ); ;; say $array[0]{'Fred'}; ;; $array[0]{'Marge'} = 'Simpson'; say $array[0]{'Marge'}; ;; dd \@array; " Flintstone Simpson [ { Barney => "Rubble", Fred => "Flintstone", Marge => "Simpson" }, ]

See also perldsc (Perl Data Structures Cookbook).

Replies are listed 'Best First'.
Re^3: Updating hash using it's reference ...
by AppleFritter (Vicar) on Jul 12, 2014 at 16:56 UTC
    Ah, I didn't know that! Thanks for the tip, my enlightened brother!
Re^3: Updating hash using it's reference ...
by perlfan (Vicar) on Jul 13, 2014 at 11:04 UTC
    I would discourage not using the postfix dereferencing, it's the first clue that something is a reference! It matters because modifying references can have side effects on the thing that is referenced!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-16 04:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found