|
49 | 49 | # but first print a confirmation for a normal exit. |
50 | 50 | if ($stdin =~ "_close_") { |
51 | 51 | print "_closed_"; |
52 | | - exit(); |
| 52 | + shutdown_procedure(); |
53 | 53 | } |
54 | 54 |
|
55 | 55 | # Read input text from STDIN: |
|
76 | 76 | interval => 0.5, |
77 | 77 | cb => sub { |
78 | 78 | if ($mode =~ "unix-epoch") { |
| 79 | + my $output_string; |
| 80 | + |
79 | 81 | if (length($input_text) == 0) { |
80 | | - print "Seconds from the Unix epoch: ".time or die; |
| 82 | + $output_string = "Seconds from the Unix epoch: ".time; |
81 | 83 | } else { |
82 | | - print "Seconds from the Unix epoch: ".time."<br>Last input: ".$input_text or die; |
| 84 | + $output_string = |
| 85 | + "Seconds from the Unix epoch: ".time."<br>Last input: ".$input_text; |
83 | 86 | } |
| 87 | + |
| 88 | + print $output_string or shutdown_procedure(); |
84 | 89 | } |
85 | 90 |
|
86 | 91 | if ($mode =~ "local-time") { |
| 92 | + my $output_string; |
87 | 93 | my $formatted_time = strftime('%d %B %Y %H:%M:%S', localtime); |
| 94 | + |
88 | 95 | if (length($input_text) == 0) { |
89 | | - print "Local date and time: ".$formatted_time or die; |
| 96 | + $output_string = "Local date and time: ".$formatted_time; |
90 | 97 | } else { |
91 | | - print "Local date and time: ".$formatted_time."<br>Last input: ".$input_text or die; |
| 98 | + $output_string = |
| 99 | + "Local date and time: ".$formatted_time."<br>Last input: ".$input_text; |
92 | 100 | } |
| 101 | + |
| 102 | + print $output_string or shutdown_procedure(); |
93 | 103 | } |
94 | 104 | }, |
95 | 105 | ); |
96 | 106 |
|
97 | 107 | $event_loop->recv; |
| 108 | + |
| 109 | +# Using a function one can implement a much complex shutdown procedure, |
| 110 | +# called when a shutdown command is received from PEB or |
| 111 | +# when PEB unexpectedly crashes and script loses its STDOUT stream. |
| 112 | +# This function must not be named 'shutdown' - |
| 113 | +# this is a reserved name for a Perl prototype function! |
| 114 | +sub shutdown_procedure { |
| 115 | + exit(); |
| 116 | +} |
0 commit comments