Guest User

Perl SIGIO example

a guest
Sep 30th, 2012
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.59 KB | None | 0 0
  1. #!/bin/env perl
  2.  
  3. # http://stackoverflow.com/q/12640993/132382
  4. #
  5. # XXX - !!Error checking removed!!
  6. #
  7. # This produces:
  8. #  -- SIGIO --
  9. #  -- SIGIO --
  10. #  -- SIGIO --
  11. #  done
  12.  
  13. use strict; use warnings;
  14. use Fcntl;
  15. use v5.16;
  16.  
  17. $SIG{IO}= sub { say "-- SIGIO --" };
  18.  
  19. my ($fh);
  20.  
  21. if (! open($fh, '-|')) { # XXX Error checking
  22.   # Child
  23.   for ('one', 'two') {
  24.     sleep 2;
  25.     say;
  26.   }
  27.   exit;
  28. }
  29.  
  30. # Parent
  31. fcntl($fh, F_SETFL, fcntl($fh, F_GETFL, 0) | O_ASYNC); # XXX Error checking
  32. fcntl($fh, F_SETOWN, 0 + $$);
  33.  
  34. # Wait a bit
  35. while (time < ($^T + 10)) {
  36.   sleep 1;
  37. }
  38.  
  39. say "done";
Advertisement
Add Comment
Please, Sign In to add comment