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

Has anyone else run into this? I using Active State perl 5.6.1 build 626 on Win2K ver 5.00.2195. If I predeclair pointers to two hashes it will merge the entries together? I'd like to use STRICT in my programing but this will pretty much kill it. Does anyone know any workarounds, patches, etc? Did I miss something basic? Below is the test code and its output.
# USING PREDECLARATIONS IT FAILS my $x=""; my $another=""; print "WITH PREDECLS\n"; $x->{"FISH2"} = "FISH2"; $another->{"NONE"}="NONE"; foreach $item (keys %{$x}) { print "ELEMENTS: $item, $x->{$item}\n"; } # NOT USING PREDECLARATIONS IT WORKS print "\n\nWITHOUT PREDECLS\n"; $x2->{"FISH2"} = "FISH2"; $another2->{"NONE"}="NONE"; foreach $item (keys %{$x2}) { print "ELEMENTS: $item, $x2->{$item}\n"; }
Here is the output:
WITH PREDECLS ELEMENTS: NONE, NONE ELEMENTS: FISH2, FISH2 WITHOUT PREDECLS ELEMENTS: FISH2, FISH2

Replies are listed 'Best First'.
Re: Active State Perl 5.6.1 build 626 Trashes hashes.
by runrig (Abbot) on Aug 08, 2001 at 20:48 UTC
    You are not predeclaring a hash nor a hash reference, you are predeclaring a scalar and giving it a character string as a value, and you cannot dereference a string. You want just either one of the following:
    # A hash reference can be autovivified from an undefined value my $x; # Or assign an empty hash reference my $x = {};
    Update: Good point, John. I don't like to think about symbolic references, so I hardly ever consider them :-)
      i made the asumption that a pointer to a hash was not treated the same as a hash. That is,$a->{'fish'} and $a{'fish'} are these the same?If not how can I reference and deeferene both. (i.e. </CODE>$a=\{}<CODE>)
        First take a trip through 'perldoc perldata'. $a->{'fish'} is dereferencing the hash reference $a.
        $a{'fish'} is referencing an element of the hash %a.
        They are two different things using two different variables, $a and %a.

        And get ready to re-learn all this when Perl 6 comes around :-)

      Actually, he is dereferenceing a string, since he's not using strict. Both variables "point" to the same place though, and that was doing confusing things.
Re: Active State Perl 5.6.1 build 626 Trashes hashes.
by rchiav (Deacon) on Aug 08, 2001 at 21:00 UTC
    And for Yet Another Reason to use strict;.. if you had included it, you would have gotten the following error..
    Can't use string ("") as a HASH ref while "strict refs" in use at ./tr +y.pl line 11.
    line 11 after I made it run under strict is..
    x->{"FISH2"} = "FISH2";

    Rich

Re: Active State Perl 5.6.1 build 626 Trashes hashes.
by John M. Dlugosz (Monsignor) on Aug 09, 2001 at 07:03 UTC
    You're not predeclaring a hash. I think you meant:
    my $x= {}; # reference to empty hash
    you assigned $x a string, which is, well, not a hash reference, as pointed out in other posts.

    If you were thinking "empty" but just getting the syntax wrong, use {} where you had "".