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