Advertisement
hakonhagland

perl-alarm-lexical-file-handle

Jan 12th, 2022
2,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.00 KB | None | 0 0
  1. use feature qw(say);
  2. use strict;
  3. use warnings;
  4. use POSIX ":sys_wait_h";
  5.  
  6. local $SIG{ALRM} = sub { die "timed out\n"; };
  7. my $pid;
  8. my $start_time = time;
  9. #my $fh2;
  10. eval {
  11.     alarm 5;
  12.     #$pid = open $fh2, '-|', "sleep", 10;
  13.     $pid = open my $fh, '-|', "sleep", 10;
  14.     #$pid = open my $fh, "sleep 10|";
  15.     #$pid = open FH, "sleep 10|";
  16.     #$pid = open FH, '-|', "sleep", 10;
  17.     say "reading line from child..";
  18.     #my $line = <$fh2>;  # hangs here for 10 seconds..
  19.     my $line = <$fh>;
  20.     #my $line = <FH>;
  21.     #close $fh2;
  22.     #close $fh;
  23.     #close FH;
  24.     say "end of eval..";
  25.     alarm 0;
  26. };
  27. say $@ if $@;
  28. say "done. elapsed = ", time - $start_time;
  29. if ( $pid ) {
  30.     say "PID = $pid";
  31.     my $kid = waitpid $pid, WNOHANG;
  32.     say "waitpid returned $kid";
  33.     if ($kid == -1) {
  34.         say "child does not exist";
  35.     }
  36.     if ($kid == 0) {
  37.         say "Killing child $pid";
  38.         kill 'KILL', $pid;
  39.         say "Done.";
  40.     }
  41. }
  42. else {
  43.     say "No child started..";
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement