This simple excerpt, run on Perl 5.16.3, at open forks a sub-process which send data to the main one.
#!/usr/bin/perl
use Tk;
my $mainWindow = MainWindow->new();
$mainWindow->Button(-text => "Run", -command => \&run)->pack();
MainLoop;
my $fh;
sub receiver()
{
print "$_" while <$fh>;
print "End\n";
close $fh;
}
sub run()
{
print "run\n";
my $pid = open $fh, '-|';
die "Failed to start\n" unless defined $pid;
print "pid=$pid\n";
exec "echo Hello" unless $pid;
$mainWindow->fileevent($fh, 'readable', \&receiver);
}
It works as expected the first time only, at the second click on the button the open produce:
Tk::Error: Can't locate object method "OPEN" via package "Tk::Event::IO" at fe.pl line 16. Tk callback for .button Tk::ANON at /usr/lib64/perl5/vendor_perl/Tk.pm line 251 Tk::Button::butUp at /usr/lib64/perl5/vendor_perl/Tk/Button.pm line 175 (command bound to event)
I'm struggling to understand the reason, without success. Any idea?