Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Problems passing hash reference

by baxy77bax (Deacon)
on Oct 11, 2019 at 16:36 UTC ( [id://11107345]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I am having problem understanding why this code is not working:

# my Test package (Test.pm) package Test; use strict; use Data::Dumper; sub new { my ($class) = @_; my $self->{_name} = __PACKAGE__; $self->{_suffix} = ".sp"; bless $self, $class; } sub make{ my ($self,$arg) = @_; if (ref $arg eq 'ARRAY'){ foreach my $t (@{$arg}){ if (ref $t eq 'HASH'){ _ss($t) }; } } } sub _ss { my ($self, %arg) = @_; print Dumper(\%arg); } 1; ## and my test scritp(test.pl) use strict; use lib "./"; use Test; my $t = Test->new(); my @a; my %t = (1 =>"1", 2 => "2"); push(@a,\%t); $t->make(\@a);
When I execute I get
$VAR1 = {};
Why ?? How to pass a hash ref to _ss()

Replies are listed 'Best First'.
Re: Problems passing hash reference
by shmem (Chancellor) on Oct 11, 2019 at 16:48 UTC
    sub make{ my ($self,$arg) = @_; if (ref $arg eq 'ARRAY'){ foreach my $t (@{$arg}){ if (ref $t eq 'HASH'){ _ss($t) }; } } } sub _ss { my ($self, %arg) = @_; print Dumper(\%arg); }

    You probably mean

    $self->_ss($t)

    since in sub _ss() you are shifting away the first argument into $self.
    What happens next? You assign the arguments to a hash, but in the calling code you just pass a single reference, not a key/value pair. So your hash reference ends up as a stringified key in %arg with no value attached.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Problems passing hash reference
by AnomalousMonk (Archbishop) on Oct 11, 2019 at 22:02 UTC

    Further to shmem's post:

    sub _ss { my ($self, %arg) = @_; print Dumper(\%arg); }

    ... so what you probably want is something like:

    sub _ss { my ($self, $hashref) = @_; print Dumper($hashref); # do stuff with $hashref }
    or maybe:
    sub _ss { my ($self, $hashref) = @_; my %hash = %$hashref; print Dumper(\%hash); # do stuff with %hash }


    Give a man a fish:  <%-{-{-{-<

Re: Problems passing hash reference
by NetWallah (Canon) on Oct 11, 2019 at 22:03 UTC
    In addition to shmemem's "$self->_ss()" call, you need to update the _ss argument to be a scalar, instead of a hash, since you are calling it with a scalar "$t".:
    sub _ss { my ($self, $arg) = @_; print Dumper($arg); }
    Update: I just noticed that AnomalousMonk's post says essentially the same thing.

    Actually, shmem points this out in his original post.

                    "From there to here, from here to there, funny things are everywhere." -- Dr. Seuss

Re: Problems passing hash reference
by Anonymous Monk on Oct 12, 2019 at 11:04 UTC
    Hi. Test is taken. /Local.*/ is always available
      Considered: [AnomalousMonk]: REAP: Meaningless
      You need some education, AnomalousMonk. Put away your itching reap finger.

      The namespace Test is already in use, so one should not use it for examples:

      perl -mTest -e'print Test->VERSION' # 1.31

      The namespace Local is reserved.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11107345]
Approved by rjt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found