#!/usr/bin/perl use strict; use warnings; use Cwd; # While running, this program waits for someone to read the file ".signature" # (which is actually a "named pipe" which was made with the "mknod" program) # and then it spits out the file ~/.sigbody, followed by a random line from # the file ~/.sigquotes. my $sigfile = '.signature'; # go to invoking user's homedir chdir; my $homedir = cwd(); while(1) { print "$homedir/$sigfile isn't a FIFO - abort!!\n" unless (-p $sigfile); # Block until there's a reader open (my $FIFO, ">$sigfile") || die "Can't open $homedir/$sigfile: $!"; # Grab a random line from .sigquotes. # (This code is not as efficient as it could be, but my .sigquotes # file is small (<250 lines) and my .signature isn't accessed often. # This code is also very clear and obvious. And has, in my experience, # given "more random" appearing results than some other code. So I # consider the tradeoff worth it in this particular case.) open (my $QFILE, ".sigquotes") or die "Can't open $homedir/.sigquotes: $!"; my @quotes = <$QFILE>; my $randquote = $quotes[int(rand (scalar @quotes))]; close $QFILE; # Spit out sig body, then quote. open (my $SIGBODY, ".sigbody") or die "Can't open $homedir/.sigbody: $!"; print { $FIFO } <$SIGBODY>; # Dumb {} required for indirect filehandle print $FIFO $randquote; close $SIGBODY; # Fin close $FIFO; sleep 2; # to avoid dup sigs }