Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

return via hash reference

by kp2a (Sexton)
on Jan 26, 2013 at 01:19 UTC ( #1015439=perlquestion: print w/ replies, xml ) Need Help??
kp2a has asked for the wisdom of the Perl Monks concerning the following question:

unexpected result hash reference passed to subroutine
filled in subroutine shown by debug print
found empty by calling routine
#! /usr/bin/perl -w use strict; use api::Mtik; use api::printhash; sub getsnmp { my $hashref = shift or die; my $debug = shift || 0; print LOG "getsnmp: entry debug $debug\n" if $debug; %$hashref = (); my @command; push @command,'/snmp/print'; push @command,'.id'; my($retval,@results) = &Mtik::talk(\@command); if ($retval != 1) { return $results[0]{'message'}; } $hashref = $results[0]; if($debug) { printhash($hashref,"getsnmp: return value"); } return ""; } 1;
calling routine
#! /usr/bin/perl -w use strict; use DBI; use bbvi::iptools; use api::Mtik; use api::printhash; use api::getsnmp; my $ip = $ARGV[0] || ''; isip($ip) or die; open LOG,">testresult.txt" or die; unless(Mtik::login($ip,'admin','xxxxxxxx')) { die "login failed\n" } my %hash = (); if(my $error = getsnmp(\%hash,1)) { printf LOG qq{%3d: get snmp failed - \"$error\"\n},__LINE__; exit; } printhash(\%hash,"hash returned from getsnmp");
debug print
getsnmp: entry debug 1 getsnmp: return value 'contact'=>'' 'enabled'=>'true' 'engine-id'=>'' 'location'=>'' 'trap-community'=>'(unknown)' 'trap-generators'=>'' 'trap-target'=>'' 'trap-version'=>'1' results hash 'contact'=>'' 'enabled'=>'true' 'engine-id'=>'' 'location'=>'' 'trap-community'=>'(unknown)' 'trap-generators'=>'' 'trap-target'=>'' 'trap-version'=>'1' hash returned from getsnmp

Comment on return via hash reference
Select or Download Code
Re: return via hash reference
by choroba (Abbot) on Jan 26, 2013 at 01:33 UTC
    Which part of the code prints "results hash"? Also, after assigning to $hashref (line 20 in the sub), $hashref no longer references %hash.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      right you are! - I have to copy rather than assign the result to the hash ref! thanks! an extra pair of eyes is always useful!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015439]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (16)
As of 2014-09-22 13:05 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (191 votes), past polls