Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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
subroutine
#! /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] || '10.105.41.166'; 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'=>'0.0.0.0' 'trap-version'=>'1' results hash 'contact'=>'' 'enabled'=>'true' 'engine-id'=>'' 'location'=>'' 'trap-community'=>'(unknown)' 'trap-generators'=>'' 'trap-target'=>'0.0.0.0' 'trap-version'=>'1' hash returned from getsnmp

Replies are listed 'Best First'.
Re: return via hash reference
by choroba (Chancellor) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2016-06-27 18:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (341 votes). Check out past polls.