Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I've run into a strange problem using a HoH within a subroutine. It appears that even though I initialize the hash and use 'my', the hash is persisting outside the scope of the sub. When I call the sub a second time instead of creating and populating a new HoH it uses the previous HoH. I would think that the HoH would be destroyed when the sub returned. Is this a bug or am I not cleaning up after myself?

Example:

my $group1 = "group1"; my $group2 = "group1"; # using primary key again bw diff values my $list1 = "1-2-3-4-5-6-7-8"; my $list2 = "1-2-3-4-5"; &test_hash($list1, $group1); &test_hash($list2, $group2); sub test_hash { my $list = $_[0]; my $group = $_[1]; my @vals = split /-/, $list; my %test_hash; $test_hash{$group} = $group; for (my $i = 0; $i < scalar @vals; $i++){ $test_hash{$group}{$vals[$i]} = $vals[$i]; } foreach $k(keys %test_hash){ print "$k\n"; foreach $v (keys %{ $test_hash{$k} }){ print "\t$test_hash{$k}{$v}\n"; } } } Output: group1 8 6 4 1 3 7 2 5 group1 # 6,7,8 should not be in the new HoH below... 8 6 4 1 3 7 2 5

Replies are listed 'Best First'.
Re: my Hash Persisting?
by Corion (Patriarch) on Jun 12, 2006 at 13:32 UTC

    You should add the line use strict; to the top of your script. After fixing the minor errors ($k and $v should be declared), Perl will then point out to you that what you maybe think you're doing is not what you are actually doing:

    Can't use string ("group1") as a HASH ref while "strict refs" in use a +t...

    The code around line 18 looks like this:

    ... $test_hash{$group} = $group; for (my $i = 0; $i < scalar @vals; $i++){ $test_hash{$group}{$vals[$i]} = $vals[$i]; } ...

    The first assignment stores a string, 'group1', in $test_hash{$group}. Then, in the loop, you're dereferencing that string, which Perl interprets as you wanting to access the global variable of that name, %group1. This global variable persists across function calls, naturally.

    Using soft references like that is dangerous because you can easily effect an action at a distance without even knowing what you're doing. The strict pragma prevents you from unknowingly using soft references.

Re: my Hash Persisting?
by Fletch (Bishop) on Jun 12, 2006 at 13:34 UTC

    If you'd enabled strict you'd have gotten a complaint that you can't use the string "group1" as a HASH ref. The problem is that your line initializing $test_hash{ $group } = $group prevents perl from autovivifying that as a hashref later when you try and use it as such in the for loop. What's happening is that you've got a symbolic reference to the hash %{ $test_hash{ $group } } (i.e. %main::group1) and that's persisting between the two calls (since it's not a lexical).

    Either don't initialize $test_hash{ $group }, or explicitly initialize it with a hashref $test_hash{ $group } = {}.

Re: my Hash Persisting?
by jhourcle (Prior) on Jun 12, 2006 at 13:33 UTC

    I'm not entirely sure why it's persisting, but you do have an oddity in your code:

    $test_hash{$group} = $group;

    You're setting the value to a string, then in the for loops, treating it as a hashref. If you remove the line, or change it an empty hashref, you should be fine:

    $test_hash{$group} = {};

    Update: I'm not sure if I should be proud for debugging the problem w/out using 'use strict', or feel like an idiot for not realizing it wasn't on the first place. I'm leaning towards the idiot side.

Re: my Hash Persisting?
by Oaty (Acolyte) on Jun 12, 2006 at 13:49 UTC
    Thank You everyone,

    Sorry for not using strict in the first place.

    I've decided to initialize the hash as you said and it's working fine now!

    Best, Oaty