Wise monks,
I seek your wisdom to enlighten me in this matter:

In short terms:
Why does the close fail, when I try to close the filehandle
before the eof is reached?

my $cmd = '/usr/bin/strings /etc/passwd'; open my $handle, "$cmd |" or die "open: $cmd: $!\n"; # read at least one line while ( <$handle> ) { last; } close $handle or die "$cmd; exit code($?): $!\n";

In longer terms, I tested several external commands and
tested each 100 times:

#!/usr/bin/perl # vi:ts=4 sw=4 et: use strict; use warnings; # these are available commands my %cmd = ( 'cat' => '/bin/cat', 'less' => '/usr/bin/less', 'more' => '/bin/more', 'strings' => '/usr/bin/strings', ); my $textfile = '/etc/passwd'; for my $k ( keys %cmd ) { my $success = 0; for ( 1 .. 100 ) { $success += open_close_pipe( $cmd{$k} ); } print "$cmd{$k}: $success/100 successful!", $/; } sub open_close_pipe { my $ext_cmd = shift; my $cmd = "$ext_cmd $textfile"; open my $handle, "$cmd |" or die "open: $cmd: $!\n"; # read at least one line while ( <$handle> ) { last; } close $handle or do { #warn "exitcode($?): $cmd: $!\n"; # to see exit code ... return 0; }; return 1; } __END__

This was my result:

$ perl read2.pl /bin/cat: 100/100 successful! /usr/bin/less: 38/100 successful! /bin/more: 31/100 successful! /usr/bin/strings: 40/100 successful!

And the results vary (with the same code):

$ perl read2.pl /bin/cat: 100/100 successful! /usr/bin/less: 16/100 successful! /bin/more: 26/100 successful! /usr/bin/strings: 32/100 successful!

When I close the handle directly after opening (comment out the while-loop), the result is like this:

$ perl read2.pl /bin/cat: 35/100 successful! /usr/bin/less: 0/100 successful! /bin/more: 41/100 successful! /usr/bin/strings: 37/100 successful!

When I read from the handle till nothing's left (comment out the last inside the while-loop), it looks fine:

$ perl read2.pl /bin/cat: 100/100 successful! /usr/bin/less: 100/100 successful! /bin/more: 100/100 successful! /usr/bin/strings: 100/100 successful!

Can you please enlighten me and tell me, why the close fails in most cases, when the handle hasn't reached eof?

I tested upon a Linux box with Perl v5.8.8 and Perl v.5.10.0.

edit: "longer terms" included into readmore


In reply to close fails(?) occasionally when closing handle of a reading pipe? by linuxer

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.