From: activeperl-boun...@listserv.activestate.com 
[mailto:activeperl-boun...@listserv.activestate.com] On Behalf Of Edwards, Mark 
(CXO)
Sent: 12 August 2012 21:49
To: activeperl@listserv.activestate.com
Subject: Testing Success of Opening Pipe in Windows

> The typical test for open is ...
>
> open(HANDLE, "something _to_open") or die "Open failed, $!";

These days, a lexically scoped handle is recommended, e.g. open my $handle...

>
> That works on Windows for a file but not a pipe.
>
> $status=open(PIPE, "hostname |");
> This works and I can read from the pipe but $status is always non-zero so I 
> don't know whether it succeeds or > fails.  Even testing for $! or $? doesn't 
> work.  $? is always -1 and $! is always " Inappropriate I/O control > 
> operation".  This works as expected on Unix be how do I test for success or 
> failure of opening the pipe?
>
> The first half of the code below works but I forced a failure on the second 
> half
>
> Code
> ----------------------------------------
> use warnings;
> use strict;
> my($pid, $pipe);
>
> print "Open pipe with real file\n";
> $pid=open(PIPE, "hostname |");
> print("OS Error: $!\n");
> print("Child Error: $?\n");
> print("PID: $pid\n");
> print "Host Name:",  <PIPE>;
> close PIPE;
> print "\n\n";
>
> print "Open pipe with fake file to force a failure\n";
> $pid=open(PIPE, "non_existent_file |");
> print("OS Error: $!\n");
> print("Child Error: $?\n");
> print("PID: $pid\n");
> print "Host Name:",  <PIPE>;
> close PIPE;
>
>
> Output
> ----------------------------------------
> =======  Open pipe with real file
> OS Error: Inappropriate I/O control operation
> Child Error: -1
> PID: 10464
> Host Name:MEDWARDS5
>
>
> ======  Open pipe with fake file to force a failure
> OS Error: Inappropriate I/O control operation
> Child Error: -1
> PID: 10468
> 'non_existent_file' is not recognized as an internal or external command,
> operable program or batch file.
> Host Name:

Perl is executing the command in a subsidiary shell (as indicated by the 'not 
recognized' output from your fake file example, which comes from the child 
process), so strictly speaking the pipe open succeeds from perl's point of view.

Note that the child process exit status ($?) will not be available until close 
has been called, which triggers the wait call.

You could try validating the command before the open call, or possibly 
capturing stderr as well (2>&1) and checking for error messages.

See the "Pipe Open" section of 'perldoc opentut' for some other suggestions.

HTH


--
Brian Raven






Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to