How to Perl compel to create copy of hash?
I have public hash and in sub{} I want to create local copy without
reference to original.
Example with not acceptable result:
our $hash;
$hash->{first} = 1;
$hash->{second} = 2;
&mysub;
print $hash->{second};
sub mysub {
my $new_hash = $hash; # make reference, not local copy
print $new_hash->{second};
$new_hash->{second} = 10;
print $new_hash->{second};
}
2
10
10
Example with success but maybe is possible to write it better:
our $hash;
$hash->{first} = 1;
$hash->{second} = 2;
&mysub;
print $hash->{second};
sub mysub {
my $new_hash;
$new_hash->{$_} = $hash->{$_} foreach (keys %{$hash}); # make local copy
print $new_hash->{second};
$new_hash->{second} = 10;
print $new_hash->{second};
}
Petr Vileta, Czech republic
(My server reject all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
2
10
2
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs