Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Getting the return code of a program opened as a pipe

Reply
Thread Tools

Getting the return code of a program opened as a pipe

 
 
Noel Sant
Guest
Posts: n/a
 
      02-09-2004
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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      02-09-2004
On Mon, 9 Feb 2004, Noel Sant wrote:
> Hi! Can anyone help, please?

<snip>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
avoid getting opened box as new? john Digital Photography 8 07-13-2009 02:51 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Accidentaly opened I-Bagle - and then opened virus vault ?? Morph Computer Information 2 02-01-2005 03:43 AM
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM
Why does IO::Pipe::END generate an EXCEPT pipe message? lvirden@gmail.com Perl Misc 1 06-02-2004 02:17 PM



Advertisments