(also note that it's a good idea to post self-contained code for relatively simple problems like this).
output:use strict; my $key; print "create hashref\n"; my $closurehash = { some_key => { name => 'fake35' } }; foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "add to hashref\n"; $closurehash->{ "test" } = "test"; # hash ref foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "delete from hashref\n"; delete $closurehash->{"fake35"}; foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; }
In other words, "test" is NOT a hash ref. Or if it is, it's a symbolic reference to some hash called "test" that isn't mentioned in your code.create hashref fake35 add to hashref Can't use string ("test") as a HASH ref while "strict refs" in use at +test.pl line 12.
If you use a real hash-ref like so:
it'll work as expected.$closurehash->{ "test" } = { name => "test"}; # hash ref
In reply to Re: add and delete to hash reference
by Joost
in thread add and delete to hash reference
by philosophia
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |