in reply to Modify a hash via its reference
So the following works:
But as soon as you assign the values to a new variable, you create a copy:use strict; my $x = 1; modify_args($x), print $x, "\n"; sub modify_args { $_[0] = "modified"; } __END__ modified
my $x = 1; modify_args($x), # doesn't work print $x, $/; sub modify_args { my ($copy) = @_; $copy = "modified"; } __END__ 1
Which means that you can change any arguments passed to a sub as long as you stick to manipulating them in @_.
You can circumvent this restriction by passing references , using a module like Data::Bind or Data::Alias), or by creating a new hash and returning that.
If you don't like the last solution (returning a new hash) you should stick to the first one, because of the principle of least surprise: When you call a sub like this: sub_name %hash you don't expect %hash to be modified.
Another possible options are prototypes (notable (\%)), but they come with many problems, and you shouldn't use them until you understood all of these problems
|
|---|