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:

my @lines = <STDIN>;
(Somebody let me know if I missed the obvious boat here; I couldn't find the way to do this.)

So, I decided to go with ctrl-C:

{ local($SIG{INT}) = 'IGNORE'; @lines = <STDIN>; }
Hmmm... that doesn't work either. It kills my script.

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:

#!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); }
So, I'm left wondering if that was supposed to work that way?


In reply to Is the signal handler supposed to work like this? by dpmott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.