in reply to [SOLVED] Capturing errors from 3-arg pipe open in ActivePerl 5.020

From the same document a few paragraphs up from the General Examples section:

Open returns nonzero on success, the undefined value otherwise. If the open involved a pipe, the return value happens to be the pid of the subprocess.

Even if the command isn't on your system the pipe is opening a subprocess and returning the pid. Here's what I see on Windows 8.

use warnings; use strict; print "here it goes...\n"; my $return_val = open(my $article, "-|", "caesar <article"); # or die "Can't start caesar: $!"; print "tried it and got [$return_val]\n";
c:\usr\pm>openprog.pl > openprog.txt 2>&1 here it goes... The system cannot find the file specified. tried it and got [14048]
Update
use warnings; use strict; use Win32::Process::List; $!=0; print "here it goes...<$!>\n"; my $return_val = open(my $article, "-|", "caesar <article"); # or die "Can't start caesar: $!"; print "tried it and got [$return_val] <$!>\n"; print "="x75,"\n"; my %list = Win32::Process::List->new()->GetProcesses(); #return +s the hashes with PID and process name foreach my $key ( keys %list ) { # $list{$key} is now the process name and $key is the PID print sprintf("%30s has PID %15s", $list{$key}, $key) . "\n" if $k +ey == $return_val; } print "="x75,"\n";
here it goes...<> The system cannot find the file specified. tried it and got [8664] <Inappropriate I/O control operation> ====================================================================== +===== cmd.exe has PID 8664 ====================================================================== +=====

Replies are listed 'Best First'.
Re^2: Capturing errors from 3-arg pipe open in ActivePerl 5.020
by ateague (Monk) on Nov 16, 2015 at 17:50 UTC

    Sorry, I feel a bit dense here, but what do I do with the PID once I have it? I get a PID regardless of whether or not the command succeeded.

    pipe.pl

    #!/usr/bin/perl use 5.018; use strict; use warnings; my $pid = open (my $ARTICLE, "-|", "caesar") or die "Can't start caesa +r: $!\n$^E"; my $read = <$ARTICLE>; say "[$read][$pid]";
    Results:
    perl pipe.pl 'caesar' is not recognized as an internal or external command, operable program or batch file. Use of uninitialized value $read in concatenation (.) or string at pip +e.pl line 9. [][1236]

      You asked in the OP what is different about your command and why isn't the 'or die' clause working in your open command. I provided documentation that shows if you open a pipe it returns the pid instead of just non-zero or zero like with opening a file. That is the difference and the answer to your question.

      I wasn't telling you to do anything with the pid, only that it is the return value and the reason why your code didn't work as expected.

      I don't think this example should be included in the documentation for open since it doesn't work as expected for pipes.