Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Global variable unexpectedly modified when passed by reference

by parv (Parson)
on Dec 08, 2014 at 15:27 UTC ( [id://1109588]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Global variable unexpectedly modified when passed by reference
in thread Global variable unexpectedly modified when passed by reference

Of course you can pass by value & make changes sans changes to original reference via dereference ...

#!/perl use warnings; use strict; use 5.010; my $got = throw_ref(); my %hash = change_hash( %{ $got } ); say 'original hash now ...'; show_hash( %{ $got } ); exit; sub change_hash { my ( %h ) = @_; say 'before modification ...'; show_hash( %h ); $h{'p'} += 10; $h{'q'} = 3; say 'after modification ...'; show_hash( %h ); return %h; } sub show_hash { my ( %h ) = @_; printf "%s : %s\n" , $_ , $h{ $_ } for sort keys %h; return; } sub throw_ref { return { 'p' => -2 } ; } __END__ before modification ... p : -2 after modification ... p : 8 q : 3 original hash now ... p : -2

... But if the hash reference is big and/or tied, then dereference could be expensive and/or undesired.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-03-29 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found