Considering the test code below, I'm wondering why I can't replace a "passed-by-reference" hashref with another hashref in my sub routine.
$|++;
my $href = { one => 1, two => 2 };
print "\nBefore:\n";
print Dumper($href);
set_element($href);
print "\nset_element:\n";
print Dumper($href);
assign_new_href($href);
print "\nAttempt to assign new hashref:\n";
print Dumper($href);
##########
sub assign_new_href {
my $href = shift;
$href = { new_key => 4 } ;
}
##########
sub set_element {
my $href = shift;
$href->{baz} = 4;
}
The output (below) shows that the attempt to assign a new hashref fails silently. ?@#$%?.
Before:
$VAR1 = {
'one' => 1,
'two' => 2
};
set_element:
$VAR1 = {
'baz' => 4,
'one' => 1,
'two' => 2
};
Attempt to assign new hashref:
$VAR1 = {
'baz' => 4,
'one' => 1,
'two' => 2
};
-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday