OS: Debian Squeeze
Net-SNMP version 5.4.3 (debian package: libsnmp-perl version 5.4.3~dfsg-2)
To reproduce: perl -MSNMP -w -e 'my $sess = SNMP::Session->new(DestHost => "bogus")'
(assuming you don't have a host in your DNS called "bogus")
If perl modules experience warning conditions, I expect them to issue a real perl warning, not print directly to STDERR! That is not trappable.
Here is a workaround that temporarily reopens STDERR for the duration of the SNMP::Session->new() call, but that is a HACK:
my $sess;
my ($tempSTDERR, $STDERRfilename) = File::Temp::tempfile();
open SAVE_STDERR, '>&STDERR';
open STDERR, '>&' . fileno($tempSTDERR);
# If SNMP::Session->new() is going to die for whatever reason, make sure we
# restore STDERR first!
eval {
$sess = SNMP::Session->new(
'Community' => 'public',
'DestHost' => 'bogus',
'Version' => '2c',
);
};
my $err = $@;
# Don't juse use
# open STDERR, '>&SAVE_STDERR';
# because it gives this warning:
# Name "main::SAVE_STDERR" used only once: possible typo
# refering to the line opening SAVE_STDERR above
open STDERR, '>&' . fileno(SAVE_STDERR);
close $tempSTDERR;
die $err if $err;
my $stderrOutputDuringSessionNew = `cat $STDERRfilename`;
unlink $STDERRfilename;
chomp $stderrOutputDuringSessionNew;
if ($stderrOutputDuringSessionNew ne '') {
warn sprintf "STDERR output from SNMP::Session->new(): '%s'",
$stderrOutputDuringSessionNew;
}
I thoroughly expect sourceforge's bug system to spoil all indents for this, so here it is on github gist too: https://gist.github.com/1210984
See Patch #1285 Added logging primitives to perl bindings for a better way to deal with this.