Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I think that this behaviour is caused by the fact that Perl has to launch a shell to process a command given in a string form, and the open doesn't fail because shell does launch successfully (it's the command that fails later):

use Test::Simple tests => 3; ok !open(my $fh, '|-', 'nonexistent program'), 'string form of open fa +ils'; ok open(my $prince, '|-', 'nonexistent program 2>/dev/null'), 'but suc +ceeds if shell uses redirection'; ok !open(my $none, '|-', 'nonexistent', 'program'), 'list form of open + fails, as it should'; __END__ 1..3 ok 1 - string form of open fails ok 2 - but succeeds if shell uses redirection ok 3 - list form of open fails, as it should

But why does string form of open correctly fail in the first test? Perl is able to process a simple command without help of a shell, as strace shows:

$ strace -f -e clone,execve perl -e'open(my $fh, "|-", "nonexistent pr +ogram")' execve("/usr/bin/perl", ["perl", "-eopen(my $fh, \"|-\", \"nonexisten" +...], [/* 45 vars */]) = 0 clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIG +CHLD, child_tidptr=0xb74f7a68) = 7792 Process 7792 attached [pid 7792] execve("/home/aitap/perl5/bin/nonexistent", ["nonexistent" +, "program"], [/* 45 vars */]) = -1 ENOENT (No such file or directory +) [pid 7792] execve("/home/aitap/scripts/nonexistent", ["nonexistent", +"program"], [/* 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] execve("/home/aitap/bin/nonexistent", ["nonexistent", "pro +gram"], [/* 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] execve("/usr/local/bin/nonexistent", ["nonexistent", "prog +ram"], [/* 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] execve("/usr/bin/nonexistent", ["nonexistent", "program"], + [/* 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] execve("/bin/nonexistent", ["nonexistent", "program"], [/* + 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] execve("/usr/local/games/nonexistent", ["nonexistent", "pr +ogram"], [/* 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] execve("/usr/games/nonexistent", ["nonexistent", "program" +], [/* 45 vars */]) = -1 ENOENT (No such file or directory) [pid 7792] +++ exited with 1 +++ --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=7792, si_uid +=1000, si_status=1, si_utime=0, si_stime=0} --- +++ exited with 0 +++
Stream redirection, however, requires a shell:
$ strace -f -e clone,execve perl -e'open(my $fh, "|-", "nonexistent pr +ogram 2>/dev/null")' execve("/usr/bin/perl", ["perl", "-eopen(my $fh, \"|-\", \"nonexisten" +...], [/* 45 vars */]) = 0 clone(Process 7809 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, +child_tidptr=0xb753ea68) = 7809 [pid 7809] execve("/bin/sh", ["sh", "-c", "nonexistent program 2>/dev +/null"], [/* 45 vars */]) = 0 [pid 7809] +++ exited with 127 +++ --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=7809, si_uid +=1000, si_status=127, si_utime=0, si_stime=0} --- +++ exited with 0 +++

Edit: spelling

In reply to Re: Can't close pipe to invalid process by aitap
in thread Can't close pipe to invalid process by cLive ;-)

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found