Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Passing hashes to sub

by PSP (Initiate)
on Jul 22, 2017 at 08:28 UTC ( [id://1195764]=perlquestion: print w/replies, xml ) Need Help??

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

my $loopCnt=0; my %prevsidstat=%{getSessStat()}; while (1) { debug("Sleeping for $ENV{SESSNAP_INTRVL}...\n"); sleep $ENV{SESSNAP_INTRVL}; my %cursidstat=%{getSessStat()}; if ((keys %cursidstat) != 0 ) { my $ComResult={sidStatComp(%prevsidstat,%cursidstat)}; my %prevsidstat=%cursidstat; } last; }

Here I am getting hash reference when %{getSessStat()} is executed. Now I want to pass two hash references to sub to process them further, however just printing them in sub getting following error. Appreciate in advance for help. Odd number of elements in anonymous hash at line 65, <SQLPLUS> line 20.

Sub code:
#***************************************************** sub sidStatComp { #***************************************************** my (%prevsidstat,%cursidstat)=@_; print "Previous\n"; for my $instsid (keys %prevsidstat) { print "$instsid => $prevsidstat{$instsid}{SQLEXECINFO}\n"; } print "Current\n"; for my $instsid1 (keys %cursidstat) { print "$instsid1 => $cursidstat{$instsid1}{SQLEXECINFO}\n"; } return 1; }

Replies are listed 'Best First'.
Re: Passing hashes to sub
by huck (Prior) on Jul 22, 2017 at 08:53 UTC

    Passing an array like in sub(%prevsidstat) flattens the hash into a list and passes the list.

    This would be better, passing the references

    my $loopCnt=0; my $prevsidstat=getSessStat(); while (1) { debug("Sleeping for $ENV{SESSNAP_INTRVL}...\n"); sleep $ENV{SESSNAP_INTRVL}; my $cursidstat=getSessStat(); if ((keys %$cursidstat) != 0 ) { my $ComResult={sidStatComp($prevsidstat,$cursidstat)}; $prevsidstat=$cursidstat; } last; } sub sidStatComp { #***************************************************** my ($prevsidstat,$cursidstat)=@_; print "Previous\n"; for my $instsid (keys %$prevsidstat) { print "$instsid =>".$prevsidstat->{$instsid}{SQLEXECINFO}."\n"; } print "Current\n"; for my $instsid1 (keys %$cursidstat) { print "$instsid1 =>". $cursidstat->{$instsid1}{SQLEXECINFO}."\n"; } return 1; }

      In both the OPed code and in huck's reply:

      my $ComResult={sidStatComp($prevsidstat,$cursidstat)}; ... sub sidStatComp { ... return 1; }
      This tries to build the anonymous hash  { 1 } (update: a reference to which is assigned to $ComResult, which is never used) that Perl is, I think, warning about. (BTW: Kudos to PSP for using warnings — and I hope strict! OTOH, it would have been nice if PSP could have pointed out which line in the posted code was line 65.)

      Update: Code examples:

      c:\@Work\Perl\monks>perl -Mstrict -MData::Dump -e "my $x = { 1 }; dd $x; " { 1 => undef } c:\@Work\Perl\monks>perl -Mstrict -Mwarnings -MData::Dump -e "my $x = { 1 }; dd $x; " Odd number of elements in anonymous hash at -e line 1. { 1 => undef }


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

      As mentioned by huck, you need to pass references to your hashes to the subroutine to avoid list flattening (and most probably to dereference them within the subroutine, depending on your needs.

      May be this was just for your testing and debugging purposes, but I would add that since your subroutine always returns 1, the $ComResult will always end up being assigned to 1, which probably does not make too much sense (except, as noted if this was only for testing purposes). My only point here is that if your subroutine is supposed to return two new hashes (or arrays), then you would also need to use references to them in order to avoid list flattening on the return values.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found