[perl]代码库
#-----------------------------
#% mkfifo /path/to/named.pipe
#-----------------------------
open ( FIFO, "< /path/to/named.pipe" ) or die $!;
while ( <FIFO> )
{
print "Got: $_";
}
close ( FIFO );
#-----------------------------
open ( FIFO, "> /path/to/named.pipe" ) or die $!;
print FIFO "Smoke this.\n";
close ( FIFO );
#-----------------------------
#% mkfifo ~/.plan # isn't this everywhere yet?
#% mknod ~/.plan p # in case you don't have mkfifo
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# dateplan - place current date and time in .plan file
while ( 1 )
{
open ( FIFO, "> $ENV{HOME}/.plan" )
or die "Couldn't open $ENV{HOME}/.plan for writing: $!\n";
print FIFO "The current time is ", scalar ( localtime ), "\n";
close FIFO;
sleep 1;
}
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# fifolog - read and record log msgs from fifo
use IO::File;
$SIG {ALRM} = sub { close ( FIFO ) };
# move on to the next queued process
while ( 1 )
{
alarm ( 0 );
# turn off alarm for blocking open
open ( FIFO, "< /tmp/log" ) or die "Can't open /tmp/log : $!\n";
alarm ( 1 );
# you have 1 second to log
$service = <FIFO>;
next unless defined $service;
# interrupted or nothing logged
chomp $service;
$message = <FIFO>;
next unless defined $message;
# interrupted or nothing logged
chomp $message;
alarm ( 0 );
# turn off alarms for message processing
if ( $service eq "http" )
{
# ignoring
}
elsif ( $service eq "login" )
{
# log to /var/log/login
if ( open ( LOG, ">> /tmp/login" ) )
{
print LOG scalar ( localtime ), " $service $message\n";
close ( LOG );
}
else
{
warn "Couldn't log $service $message to /var/log/login : $!\n";
}
}
}
#-----------------------------
use POSIX qw ( :errno_h );
$SIG {PIPE} = 'IGNORE';
# ...
$status = print FIFO "Are you there?\n";
if ( !$status && $! == EPIPE )
{
warn "My reader has forsaken me!\n";
next;
}
#-----------------------------
use POSIX;
print _POSIX_PIPE_BUF, "\n";
#-----------------------------