pipes problem

B

Billy N. Patton

I have this bit of code:

The followfing sets $stat correctly : to 0
my $stat = 0;
$ret = undef;
$ret = `$command`;
$stat = $? >> 8;


The follow sets $stat to 1, Incorrect.
# open PIPE , "$command 2>&1 |";
# while (<PIPE>)
# {
# Lis($_);
# $ret .= $_;
# }
# $stat = close PIPE;

in book "Programming Perl" @nd Edition , Covers perl5
Page 342. Last 5 lines
the exit sataus of the child process is haaarvested by the parent
process when it eventually doeas a wait(2) system call. But this
happens in the close function, not in the open function. And that's why
you have to check the return vallue of your close function.


I tried this and got the proper return code:
close PIPE;
$stat = $? >> 8;

Even this worked:
close PIPE;
$stat = $?;


I don't understand?????

My goal here is to execute a system command and be able to capture the
stdout/stderr as it is happening. I don't like programs that simply sit
and give you no indication anything is happening.


--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
S

Scott W Gifford

[...]
The follow sets $stat to 1, Incorrect.
# open PIPE , "$command 2>&1 |";
# while (<PIPE>)
# {
# Lis($_);
# $ret .= $_;
# }
# $stat = close PIPE;

The answer is in the documentation for the close function:

If the file handle came from a piped open "close" will
additionally return false if one of the other system
calls involved fails or if the program exits with
non-zero status. (If the only problem was that the
program exited non-zero $! will be set to 0.) Closing
a pipe also waits for the process executing on the pipe
to complete, in case you want to look at the output of
the pipe afterwards, and implicitly puts the exit
status value of that command into $?.

So your only guarantee is that close will return true if $command
didn't start or exited nonzero. To find out why it failed, you need
to look at $! and $?.

----ScottG.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top