libraries are not re-entrant; particularly, memory allocation and I/O
routines are not. That meant that doing nearly I<anything> in your
handler could in theory trigger a memory fault and subsequent core
-dump - see L<Deferred Signals> below.
+dump - see L</Deferred Signals (Safe Signals)> below.
The names of the signals are the ones listed out by C<kill -l> on your
system, or you can retrieve them from the Config module. Set up an
sleep 2; # to avoid dup signals
}
-=head2 Deferred Signals
+=head2 Deferred Signals (Safe signals)
In Perls before Perl 5.7.3 by installing Perl code to deal with
signals, you were exposing yourself to danger from two things. First,
Note that the default in Perl 5.7.3 and later is to automatically use
the C<:perlio> layer.
+Note that some networking library functions like gethostbyname() are
+known to have their own implementations of timeouts which may conflict
+with your timeouts. If you are having problems with such functions,
+you can try using the POSIX sigaction() function, which bypasses the
+Perl safe signals (note that this means subjecting yourself to
+possible memory corruption, as described above). Instead of setting
+C<$SIG{ALRM}> try something like the following:
+
+ use POSIX;
+ sigaction SIGALRM, new POSIX::SigAction sub { die "alarm\n" }
+ or die "Error setting SIGALRM handler: $!\n";
+
=item Signals as "faults"
Certain signals e.g. SEGV, ILL, BUS are generated as a result of
=back
+If you want the old signal behaviour back regardless of possible
+memory corruption, set the environment variable C<PERL_SIGNALS> to
+C<"unsafe"> (a new feature since Perl 5.8.1).
+
=head1 Using open() for IPC
Perl's basic open() statement can also be used for unidirectional
# add error processing as above
$pid = open(KID_TO_WRITE, "|-");
- $SIG{ALRM} = sub { die "whoops, $program pipe broke" };
+ $SIG{PIPE} = sub { die "whoops, $program pipe broke" };
if ($pid) { # parent
for (@data) {
$client->autoflush(1);
print $client "Welcome to $0; type help for command list.\n";
$hostinfo = gethostbyaddr($client->peeraddr);
- printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
+ printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost;
print $client "Command? ";
while ( <$client>) {
next unless /\S/; # blank line