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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |