dpmott has asked for the wisdom of the Perl Monks concerning the following question:
The short question is this: should spawning a thread change how signal handlers behave? See below for a working code snippet.
So, I was banging my head against the keyboard trying to remember how to read in a series of lines from STDIN with Win32 ActivePerl. After much googling, I realize that I've only ever gotten characters with Term::ReadKey, and that isn't even close to what I wanted. Apparently, nothing is.
All I wanted to do was this, and be able to type ctrl-D to signify that I was done:
(Somebody let me know if I missed the obvious boat here; I couldn't find the way to do this.)my @lines = <STDIN>;
So, I decided to go with ctrl-C:
Hmmm... that doesn't work either. It kills my script.{ local($SIG{INT}) = 'IGNORE'; @lines = <STDIN>; }
I soon came to realize that I'm using that nifty new Perl v5.8.8 with "safe" signals, and I suspect that this was getting in the way. Sadly, Perl::Unsafe::Signals isn't available via ppm for ActivePerl, so I didn't get to find out either way.
What I did discover, quite by accident was that spawning a thread changes the behavior of the signal handler:
So, I'm left wondering if that was supposed to work that way?#!perl -w use strict; use threads; my @lines; print "Enter CTRL-C to end input\n"; $SIG{INT} = 'IGNORE'; # making this local() causes ctrl-C to kill the +script @lines = <STDIN>; threads->create( sub{} )->join(); $SIG{INT} = 'DEFAULT'; # putting this after thread creation makes eve +rything happy chomp @lines; # kill newlines from STDIN entries print "Enter CTRL-C to exit\n"; local($") = ', '; while ( 1 ) { print "You entered: @lines\n"; sleep(1); }
|
|---|