Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Global variable unexpectedly modified when passed by reference

by wee (Scribe)
on Dec 10, 2014 at 23:00 UTC ( [id://1109979]=note: print w/replies, xml ) Need Help??


in reply to Global variable unexpectedly modified when passed by reference

The global %hash is being altered because you are making alterations to its reference. What you want to do is dereference it in mysub(), like so:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = (a => 1, b => 2); mysub(\%hash); print Dumper(\%hash); sub mysub { my $h = shift; my %new_hash = %$h; delete($new_hash{'b'}); }
You can do whatever you want to %new_hash and nothing will happen to %hash.
  • Comment on Re: Global variable unexpectedly modified when passed by reference
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found