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

hi,

well this is probably the obvious one but i'm stuck and i don't now how to get out from it. i have a subroutine (obj. method) in a class that creates two hashes which i want to pass into my main script(program).

package Tool; use strict; sub new { # construct... } sub _init{ # initialise... } sub create { my $self = @_; bla bla... create %hash1 , %hash2 ... my @vi = qw(\%hash1 \%hash2); return @vi; } ############## main program ################# use strict; use lib "./temp" use Tool; ... my $tree = Tool->new(); my @array = $tree->create(); #i did the separation of variables on purpose ... my $first = $array[0]; my $second = $array[1]; my %hashfirst = %$first; my %hashsecond = %$second; ######## and now it dumps %hashfirst on <b>" print Dumper(%hashfirst)"</b>; but it doesn't want to dump the second one (%hashsecond)...
also if i pass the hashes directly it uses 3x the memory , but in this way only 80MB RAM. so my question is why does this happen?

robert

Replies are listed 'Best First'.
Re: passing the reference from a class.
by Corion (Patriarch) on Jul 20, 2008 at 20:00 UTC

    As you don't show us in what way Perl "does not want" to dump the second one, it's kinda hard to guess what happens in the code you've omitted. But there is one line that doesn't do what you might think it does:

    sub create { my $self = @_; ...

    Here, $self will likely have the value 1, because that's the number of elements in @_. If you want to assign the first value in @_ to $self, you need parentheses:

    my ($self) = @_;
      sory the message is :

      can't use string ("\%hashsecond") as a HASH reference while "strict refs" in use

      i mean it is obvious but i don't know why aoes dthi happen only the second time

      and thanks for the tip it is a typo :)

        It works as it should for me:

        use strict; sub create { my $self = @_; my (%hash1,%hash2); my @vi = qw(\%hash1 \%hash2); return @vi; } my @array = create(); #i did the separation of variables on purpose ... my $first = $array[0]; my $second = $array[1]; my %hashfirst = %$first; my %hashsecond = %$second; __END__ Can't use string ("\%hash1") as a HASH ref while "strict refs" in use +at tmp.pl line 20.

        This is because you're returning strings instead of references:

        my @vi = qw(\%hash1 \%hash2);
        should be
        my @vi = (\%hash1, \%hash2);