http://www.perlmonks.org?node_id=262776


in reply to OO Perl: Methods To Reference Hash

( ..., hshAgg => %hshAgg ) ... return $self->{hshAgg}; ... $self->{hshAgg} => %hshAgg;
You can't do that. A hash can only have strings for keys, and scalars for values. You can put REFERENCES to hashes in as the values, but then you need to access them accordingly.
hshAgg => \%hshAgg ... return %{$self->{hshAgg}}; ... $self->{hshAgg} => \%hshAgg;
Even yet, returning a whole hash isn't nice; you should return a reference to it, but then your caller has to be modified to expect a reference, too.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Re: OO Perl: Methods To Reference Hash
by P0w3rK!d (Pilgrim) on Jun 03, 2003 at 21:00 UTC
    My hash is being created correctly and looks something like this:
    HASH DUMP: 2030000 => 2030000.xml HASH DUMP: 2030001 => 2030001.xml HASH DUMP: 2030002 => 2030002.xml
    When I attempt to dump it is after the object has been instantiated with the hash in it using a set() method.
    my %hshAgg = $self->get_hshAgg(); dumpHash(%hshAgg);
    ...the dump looks like this:
    HASH DUMP: HASH(0x223db34) =>
    What am I doing wrong here?
    sub run { my $self = Myobj->new(@_); ... $self->set_hshAgg(%hshAgg); ... } # method within object sub myProblem { my %hshAgg = $self->get_hshAgg(); dumpHash(%hshAgg); }

    -P0w3rK!d

      Whenever you're trying to debug data structures, I recommend using Data::Dumper. This will help you see the structures in Perl syntax, and will avoid any bugs you may introduce in your own home-spun "dumping" methods.

      use Data::Dumper; ... print Dumper \%hashFoo; print Dumper $refHashFoo;

      Other useful learning materials are in perlreftut and perllol.

      --
      [ e d @ h a l l e y . c c ]

        The problem is not with the data, it is with the set() and get() methods that manipulate the hash within the object.
Re: Re: OO Perl: Methods To Reference Hash
by P0w3rK!d (Pilgrim) on Jun 03, 2003 at 20:42 UTC
    Ah...thank you. I *did* try referencing via '\', but could not figure out the get() method specifically.

    Thank you :)

    -P0w3rK!d