Getting the return code of a program opened as a pipe

N

Noel Sant

Hi! Can anyone help, please?

I'm using AciveState's "perl v5.8.0 built for MSWin32-x86-multi-thread"
(from a "perl -v" command), on Windows 2000. I want to use WZZIP (WinZip's
command line utility) to back up some data files. I'm using its -i option to
pick up files that have the archive attribute set, and the -rp option to
recurse into subfolders. I want to keep WZZIP's messages, but after the
first backup, they are mainly the names of folders, which aren't interesting
(to me). So I thought I'd open WZZIP as a pipe and read in its output,
filter out any lines ending with a (forward or back) slash, and print them
out again. I'd also like to print out its return code.

The following program snippet does whar I want, except for the return code.
When I just used the perl system function, it was fine, of course, but
something in the camel book (p. 342) made me think I might get it when
closing the pipe. Perhaps that's only in Unix systems. All I get is "1",
whereas WZZIP gives "0" if OK.

(The program would look prettier if I could use a non-proportional font, but
my Outlook Express News reader only seems to allow that if I use HTML, but I
wasn't sure if that was OK in this news group.)

=====================
# my $rc = system($command);
open ZIP_OUTPUT, "$command |"
or die "Error openng WZZIP output pipe etc."\n";
while (<ZIP_OUTPUT>) {
chomp;
s/\s+$//; # trim blanks right
next if /\\$/;
next if /\/$/;
print MESSAGES $_ . "\n";
}
my $rc = close ZIP_OUTPUT;
=====================

TIA,

Noel
 
P

Paul Lalli

Hi! Can anyone help, please?
The following program snippet does whar I want, except for the return code.
When I just used the perl system function, it was fine, of course, but
something in the camel book (p. 342) made me think I might get it when
closing the pipe. Perhaps that's only in Unix systems. All I get is "1",
whereas WZZIP gives "0" if OK.
=====================
# my $rc = system($command);
open ZIP_OUTPUT, "$command |"
or die "Error openng WZZIP output pipe etc."\n";
while (<ZIP_OUTPUT>) {
chomp;
s/\s+$//; # trim blanks right
next if /\\$/;
next if /\/$/;
print MESSAGES $_ . "\n";
}
my $rc = close ZIP_OUTPUT;
=====================

This is exactly what close is supposed to do. From perldoc -f close:

"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 if the external program is successful, close returns a true value. If
not, it returns a false value. The program's exit status is stored in $?
regardless.

Paul Lalli
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top