signal handler. When you want to tell the daemon to re-read the file
you simply send it the C<SIGHUP> signal.
-The implementation of such a mechanism in Perl using a normal signal
-handler works only the first time the signal is sent. The solution to
-this problem is to use C<POSIX> signal handlers if available.
+Not all platforms automatically reinstall their (native) signal
+handlers after a signal delivery. This means that the handler works
+only the first time the signal is sent. The solution to this problem
+is to use C<POSIX> signal handlers if available, their behaviour
+is well-defined.
The following example implements a simple daemon, which restarts
itself every time the C<SIGHUP> signal is received. The actual code is
info to show that it works and should be replaced with the real code.
#!/usr/bin/perl -w
-
+
use POSIX ();
use FindBin ();
use File::Basename ();
use File::Spec::Functions;
-
+
$|=1;
-
+
# make the daemon cross-platform, so exec always calls the script
# itself with the right path, no matter how the script was invoked.
my $script = File::Basename::basename($0);
my $SELF = catfile $FindBin::Bin, $script;
-
+
# POSIX unmasks the sigprocmask properly
my $sigset = POSIX::SigSet->new();
my $action = POSIX::SigAction->new('sigHUP_handler',
$sigset,
&POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGHUP, $action);
-
+
sub sigHUP_handler {
print "got SIGHUP\n";
exec($SELF, @ARGV) or die "Couldn't restart: $!\n";
}
-
+
code();
-
+
sub code {
print "PID: $$\n";
print "ARGV: @ARGV\n";
# NOTREACHED
}
+Since Perl 5.8.0, you can also use the list form of C<open> for pipes :
+the syntax
+
+ open KID_PS, "-|", "ps", "aux" or die $!;
+
+forks the ps(1) command (without spawning a shell, as there are more than
+three arguments to open()), and reads its standard output via the
+C<KID_PS> filehandle. The corresponding syntax to read from command
+pipes (with C<"|-"> in place of C<"-|">) is also implemented.
+
Note that these operations are full Unix forks, which means they may not be
correctly implemented on alien systems. Additionally, these are not true
multithreading. If you'd like to learn more about threading, see the
There's a lot more to networking than this, but this should get you
started.
-For intrepid programmers, the indispensable textbook is I<Unix Network
-Programming> by W. Richard Stevens (published by Addison-Wesley). Note
-that most books on networking address networking from the perspective of
-a C programmer; translation to Perl is left as an exercise for the reader.
+For intrepid programmers, the indispensable textbook is I<Unix
+Network Programming, 2nd Edition, Volume 1> by W. Richard Stevens
+(published by Prentice-Hall). Note that most books on networking
+address the subject from the perspective of a C programmer; translation
+to Perl is left as an exercise for the reader.
The IO::Socket(3) manpage describes the object library, and the Socket(3)
manpage describes the low-level interface to sockets. Besides the obvious