newbie syntax question

James W Walden jamesw at
Tue Apr 10 12:22:44 CDT 2001

On Tue, 10 Apr 2001, Roth, Gabrielle wrote:
> Here's the relevant snippet with line numbers:
>     21  if (open (HINFO, "$snmpget '$router' '$community' '$oid' 2>/dev/null
> |")) {
> 1 - (probably more of a unix question) the redirection to /dev/null:  I know
> this isn't the output of the command, because that's available via <INFO>
> for the parsing loop.  So what is this for?  Is it dumping the exit status?
> Is it necessary?  It works without it, but I'm wondering if it's some error
> handling thing that I can't test and won't know if it's broken until it
> breaks. :)

Filehandle 2 is almost always STDERR so he's ignoring error output.

> 2 - what is the pipe for at the end of the command?  I've tried it without
> this and I get this error:
> failed to run /usr/bin/snmpget at ./mrtg-cpustate line 35.

The pipe symbol at the end of a command tells perl that you want to access
the output of the command in your filehandle (HINFO).  A pipe at the beginning
would indicate that you want to access the input of a command.  You can't
put pipes at both the beginning and end of the command, but if you want to
access both the input and output of a command, you can use the IPC::Open2
and IPC::Open3 modules.

Finally, I noticed that the code is missing this line:

close HINFO or die "Command '$snmpget' failed with error code " . $?<<8 . "\n";

When running a command, perl's open forks a subprocess where the command is
run.  The open call can only see the return value of the fork() and so if it
succeeds, all you know is that the fork() call returned successfully.  You
don't know whether the command executed successfully until you close the

James Walden                      |  "Fall leaves blanket ground
Sr Internet Software Engineer     |   Redmond dreams darkly, beware
IMS, JFT-104, B-7                 |   Winter brings penguins"
(503) 712-2241                    |     -- Kevin Hackman


More information about the Pdx-pm-list mailing list