in reply to Re^3: Print inside SIGNALS
in thread Print inside SIGNALS

This code does not work...

alarm 2; $SIG{ALRM} = \&Finish; my $a=<>; sub Finish { print "Timeout reached"; }

This code does not work either...

alarm 2; $SIG{ALRM} = \&Finish; while (1==1){}; sub Finish { print "Timeout reached"; }

So i guess many calls are not interrumptibles... am i right>?

Replies are listed 'Best First'.
Re^5: Print inside SIGNALS
by haukex (Archbishop) on Jul 16, 2018 at 20:20 UTC
    So i guess many calls are not interrumptibles... am i right?

    I don't think that's the problem here - see my reply to your nearly identical post here.

Re^5: Print inside SIGNALS
by ikegami (Patriarch) on Jul 17, 2018 at 11:22 UTC

    First snippet: <> can't be interrupted because there's no way to check <> for errors. However, the signal handler does get called once it returns.

    Second snippet: There's nothing to interrupt here, so the signal handler gets called immediately. However, since the program has no way of terminating the program cleanly, the message will never leave STDOUT's buffer to the underlying file system handle.

      First snippet: <> can't be interrupted because there's no way to check <> for errors. However, the signal handler does get called once it returns.

      At least on my machine (Linux), it seems to be the same buffering issue...

      $ perl -wMstrict -e 'alarm 2; $SIG{ALRM}=sub{ print "Timeout reached" };<>' ^C $ perl -wMstrict -e 'alarm 2; $SIG{ALRM}=sub{ print "Timeout reached\n" };<>' Timeout reached ^C $ perl -wMstrict -e 'alarm 2; $SIG{ALRM}=sub{ print STDERR "Timeout reached" };<>' Timeout reached^C

        No, "it didn't work" for you because you killed the program before the signal handler was called.