in reply to Re^12: Print inside SIGNALS
in thread Print inside SIGNALS
I don't care much about what ikagami said exactly, but you did kill the program is what he noticed, and that is what I focus on. As far as I'm concerned that is the reason why your example does not print anything. Maybe a piece of code will help:
use strict ; use warnings ; alarm 2 ; $SIG{'ALRM'} = sub{ print "Timeout reached" } ; $SIG{'INT'} = sub { exit(0) } ; <> ; __END__ [/tmp] # perl pltst1.pl ^C[/tmp] # perl pltst1.pl ^CTimeout reached[/tmp] # (1. ctrl-c pressed < 2 seconds 2. ctrl-c pressed > 2 seconds)
1. So you see, it does matter if you kill the program. 2. Whether something is being outputted to STDOUT is not a secondary issue. It matters everything. STDERR and STDOUT buffer mechanisms are fundamentaly different.
edit: So if people are down-voting this that is fine, but then at least show me where I am wrong please.
edit 2: Examples added regards the difference between STDOUT and STDERR below (Pevious example was Linux, this was on Windows):
WITH syswrite STDERR and print STDERR:
use strict ; use warnings ; alarm 2 ; $SIG{ALRM} = sub{ syswrite STDERR, "s Timeout reached " ; print STDERR "p Timeout reached " ; } ; sleep(4) ; syswrite STDERR, "s Program end " ; print STDERR "p Program end " ; sleep(2) ; __END__ C:\perlproject>perl sigtst.pl s Timeout reached p Timeout reached s Program end p Program end s Timeout reached - Printed after 2 seconds p Timeout reached - Printed after 2 seconds s Program end - Printed after 4 seconds p Program end - Printed after 4 seconds
WITH syswrite STDOUT and print STDOUT:
use strict ; use warnings ; alarm 2 ; $SIG{ALRM} = sub{ syswrite STDOUT, "s Timeout reached " ; print STDOUT "p Timeout reached " ; } ; sleep(4) ; syswrite STDOUT, "s Program end " ; print STDOUT "p Program end " ; sleep(2) ; __END__ C:\perlproject>perl sigtst.pl s Timeout reached s Program end p Timeout reached p Program end s Timeout reached - Printed after 2 seconds s Program end - Printed after 4 seconds p Timeout reached - Printed after 6 seconds p Program end - Printed after 6 seconds
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^14: Print inside SIGNALS
by haukex (Archbishop) on Jul 19, 2018 at 21:17 UTC | |
by Veltro (Hermit) on Jul 19, 2018 at 21:46 UTC | |
by haukex (Archbishop) on Jul 19, 2018 at 22:51 UTC | |
by ikegami (Patriarch) on Jul 21, 2018 at 01:12 UTC | |
by ikegami (Patriarch) on Jul 20, 2018 at 18:48 UTC | |
by haukex (Archbishop) on Jul 20, 2018 at 19:02 UTC | |
by ikegami (Patriarch) on Jul 21, 2018 at 01:18 UTC | |
by haukex (Archbishop) on Jul 20, 2018 at 18:56 UTC |