Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015439]
Approved by ww
[james28909]: it assigns the output of yt-dl directly to a var, that way there is no files written to disk, and the var is removed in the end
[james28909]: @choroba yes they do but for some reasom when i try to pipe the output of youtube-dl to ffmpeg, it fails. hence the work around lol
[RonW]: james28909: Interesting trick
[choroba]: The theme is "High-end Perl", so both my talks are antitheses: "Low-end Perl" and "How to almost win a T-shirt in a programming contest"
[RonW]: I wonder if that's a CMD.exe limitation or a Windows limitation
[james28909]: what i am trying to accomplish is piping the output of ffmpeg to yet another encryption routine using libsodium. and hopefully once that is done i will be able to forward the packets to discord servers
[RonW]: choroba: Please define "high end" and "low end"
[LanX]: choroba: subversive (as usual ;)
[james28909]: i dunno, if i didnt have so little experience in linux i would swap. but it would be to much of a learning curve for me right now.
[choroba]: Low-end is defined as the Perl that generates millions of income

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (16)
As of 2017-05-22 21:29 GMT
Find Nodes?
    Voting Booth?