Getting the return code of a program opened as a pipe

Discussion in 'Perl Misc' started by Noel Sant, Feb 9, 2004.

  1. Noel Sant

    Noel Sant Guest

    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
     
    Noel Sant, Feb 9, 2004
    #1
    1. Advertising

  2. Noel Sant

    Paul Lalli Guest

    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
     
    Paul Lalli, Feb 9, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    489
    Jack Klein
    Nov 2, 2004
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    493
    Richard Bos
    Jun 18, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    827
    Keith Thompson
    Mar 6, 2005
  4. dj
    Replies:
    0
    Views:
    449
  5. Replies:
    1
    Views:
    225
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page