The following mundane piece of code does piped opens to 3 external processes that will each run for 3 seconds, and then times them out after 2, 3 & 4 seconds respectively. It gathers whatever output they produce before being timed out and (attempts) to return it to the caller.
UPDATE: Code and trace below updated to reflect andal's post below, and the information that error 1 below is fixed in 5.16. Error 2, the "modification of readonly value" remains; though in a slightly changed form as reflected by the new trace.
See the console trace after the __END__ token for the results::
#! perl -slw use strict; use threads stack_size => 4096; use threads::shared; sub timedCommand { my( $timeout, $cmd, @args ) = @_; my $pid :shared; my @input :shared; async { # local $_; ### LINE C ### $pid = open my $fh, "$cmd @args |" or die "$!, $^E"; push @input, $_ while <$fh>; }->detach; sleep 1 until $pid; sleep 1 while kill 0, $pid and $timeout--; kill 3, $pid if $timeout; print "@input"; ### LINE A ### return @input; } my $cmd = q[ perl -E"$|++; sleep(1), say for 1 .. $ARGV[0]" ]; print timedCommand( 3, $cmd, $_ ) for 2, 3, 4; ### LINE B ### __END__ C:\test>\perl64-16\bin\perl.exe timedCommand.pl Thread 1 terminated abnormally: Modification of a read-only value atte +mpted at timedCommand.pl line 15. Thread 2 terminated abnormally: Modification of a read-only value atte +mpted at timedCommand.pl line 15.
The two mysteries are:
Update: This part is fixed in 5.16.
C:\test>timedCommand.pl Thread 1 terminated abnormally: Modification of a read-only value atte +mpted at C:\test\timedCommand.pl line 15. Thread 2 terminated abnormally: Modification of a read-only value atte +mpted at C:\test\timedCommand.pl line 15. Thread 3 terminated abnormally: Modification of a read-only value atte +mpted at C:\test\timedCommand.pl line 15.
UPDATE: This problem remains, though with slightly different output. Only two copies of teh error message are produced.
Perl bugs or bad code?
In reply to One piece of code, two mysteries! (UPDATED.) by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |