Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

I want to capture a programs output AND I want the return status

by Anonymous Monk
on Apr 12, 2001 at 19:57 UTC ( [id://72076]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Good afternoon

Apologies if this is answered already, I've had a look but can't find it.

I'd like to capture the ouput of a windows batch file so I can write it out to a logfile. Common wisdom says use backtics.

But I'd also like to know if it blew up by testing the return status, which suggests using a system() call

Are they mutually exclusive, or can I get both the return value AND the programs output?

Best wishes

Reg
  • Comment on I want to capture a programs output AND I want the return status

Replies are listed 'Best First'.
Re: I want to capture ...
by stefan k (Curate) on Apr 12, 2001 at 20:06 UTC
    suggest: man perlvar

    And you'll find

    $CHILD_ERROR $? The status returned by the last pipe close, backtick (``) command, or system() operator. Note that this is the status word returned by the wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is actually ($? >> 8), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump. (Mnemonic: similar to sh and ksh.)
    ... and so forth..
    That should help I'd think.

    Regards Stefan K

Re: I want to capture a programs output AND I want the return status
by davorg (Chancellor) on Apr 12, 2001 at 20:07 UTC

    Use backticks and $?. From perldoc perlvar:

    $? - The status returned by the last pipe close, backtick (``) command, or system() operator. Note that this is the status word returned by the wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is actually ($? >> 8), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump. (Mnemonic: similar to sh and ksh.)

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: I want to capture a programs output AND I want the return status
by Asim (Hermit) on Apr 12, 2001 at 20:08 UTC
Re: I want to capture a programs output AND I want the return status
by arhuman (Vicar) on Apr 12, 2001 at 20:02 UTC
    Why not using the program output as a 'logical return value' ?
    (will show you if the program 'worked' and not only if the program excuted correctly...)

    For example :
    Modify your prog to display 'ok' at the end, if everything is ok.
    In the case of a program that you can't modify, a regex matching a typical message for success/correct behaviour will be used.
    (obiously no/truncated/incorrect output -> pb)

    UPDATE : Thinking twice about it, it sounds a really weird way...
    UPDATE2 (after stefan_k and davorg answers): Now that it's clear that it's not the usual (correct?) way to do it
    (use of $? is the right answer) could someone spot a flaw/weakness/drawback in my idea ?
    And tell me why it's not used more often...

    "Only Bad Coders Badly Code In Perl" (OBC2IP)
      Several issues.

      First of all it is different. Unless you control the external utility (which you probably don't because you didn't write it) you cannot make a change like that. And if you do you will confuse everyone else.

      Secondly it is inflexible. There are lots of programs that have legitimate reasons to spit out reams of text that may include an OK in them.

      Thirdly you often want to send a child program's output somewhere else but you still need to capture the return code.

      That answer why your idea is not used more often?

Re: I want to capture a programs output AND I want the return status
by Desdinova (Friar) on Apr 12, 2001 at 20:11 UTC
    Rather than call a batch file form perl why don't you try reimplenting the batch files's functions in Perl. You can do just about anything in Perl that you can with the batch file even if you have to resort to the system call.
    This will let you do logging as well as catching errors. If fact you will be able to capture step by step status and respond to them in different way. It will also be easier to clean up the output for use in a log file.
Re: I want to capture a programs output AND I want the return status
by geektron (Curate) on Apr 13, 2001 at 03:38 UTC
    sounds like a job for IPC::Open2 or IPC::Open3. specifically, Open3, which will catch the STDIN, STDOUT, and STDERR from a program/ process you could normally run with backticks or a call to system

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-16 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found