I have a file where I run a shell command into a pipe. It looks like this:

#!/usr/bin/perl -w use strict; use Net::SSH::Expect; use TestExt; use Data::Dumper; package Test; sub run { my $cmd = 'ls -al'; my $pid = open OUT, "$cmd |" or die "cannot fork: $!"; print "running \"$cmd\" (pid = $pid) ... \n"; my @a = <OUT>; print "closing ... \n"; close OUT or die "cannot close: $! $?"; print @a; } package main; my @a = Test->run(); print @a;

The reason for the package Test will become clear in a moment. Now, I do the same thing but with the run method in an external file. here it is:

package TestExt; sub run { my $cmd = 'ls -al'; my $pid = open OUT, "$cmd |" or die "cannot fork: $!"; print "running \"$cmd\" (pid = $pid) ... \n"; my @a = <OUT>; print "closing ... \n"; close OUT or die "cannot close: $! $?"; print @a; } 1;

Now change the call to "TestExt->run()" and things stop working. When "CLOSE" is called, you get "cannot close: No child processes -1" as an error.

Now, if I remove the "use Net::SSH::Expect", everything works fine, both ways.

This is on a CentOS system, with a standard perl 5.8.8. It seems like quite an odd interaction, (why would having the code in an external module affect things?) and I suspect that it is a bug in Net::SSH::Expect. But if anyone here has any ideas about this, or can verify it on other systems, I'd be very interested to hear them before I file a report. Of course, there could as well be some silly error in there that I have not spotted, too.


In reply to odd interaction of Net::SSH::Expect and open by danmcb

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.