Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Signature rotation.

by hagus (Monk)
on May 23, 2002 at 05:59 UTC ( #168673=snippet: print w/replies, xml ) Need Help??
Description: A really short little ditty to take a file delimited by '%' characters (on lines by themselves) then randomly select an entry, then print it to the .signature file.

Literally taken straight from my ~/scripts directory, all still hardcoded. Pick it to pieces, but remember it's just a hack :)

First few lines of my .signature.rot file. Note the first line is blank, and there needn't be a trailing newline at the end:

Leo: (July 23 - Aug. 22) When driving through the desert, you should never stop and eat mysterious piles of birdseed, even if "FREE BIRDSEED" signs are stuck in them. % "Reality is that which, when you stop believing in it, doesn't go away". -- Philip K. Dick % So, Lone Starr, now you see that evil will always triumph, because good is dumb. -- Dark Helmet % Thank you for pressing the self-destruct button. This ship will self-destruct in three minutes. % Do you make up these questions, Mr. Holden, or do they write them down for you? % Yes, questions. Morphology, longevity, incept dates. % "Begun, this clone war has." Donate to the House With No Sentence Structure. Dedicated to assisting grammatically disadvantaged Jedi for over 900 years.

use IO::File;

$/ = "%";

open(FILE, "/home/hagus/.signature.rot");
while (<FILE>)
    $tmp = $_;
    $tmp =~ s/%//g;
    push @a, $tmp;

my $fh = new IO::File("/dev/urandom", "r");

my $foo = $fh->read($value, 100);


my $num = unpack("S*", $value);

my $index = $num % (@a + 0);

open(FILE, ">/home/hagus/.signature");
print FILE $a[$index] . "\n";
Replies are listed 'Best First'.
Re: Signature rotation.
by Juerd (Abbot) on May 23, 2002 at 07:12 UTC

    $/ = "%";

    Why not "\n%\n"?

    Oh, and just because I like Tie::File and strict, and dislike using /dev/urandom, here's an alternative implementation:

    #!/usr/bin/perl -w use Tie::File; use IO::File; use Fcntl qw(O_RDONLY); use strict; chdir '/home/hagus'; tie my @fortunes, 'Tie::File', '.signatures.rot', mode => O_RDONLY, re +csep => "\n%\n"; IO::File->new('.signature', 'w')->print($fortunes[rand @fortunes]);

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

      Great! I like this.

      I've never used Tie before actually, so this is good sample code for me.

      Why not "\n%\n" ? Because I coded that in about 3 nanoseconds, and it didn't even occur to me. Maybe some C oriented portion of my brain baulked at using more than one character as a separator?!

      I think I had a reason for using /dev/urandom instead of rand. Who wants to remind me what it was? I've forgotten.

      Ash OS durbatulk, ash OS gimbatul,
      Ash OS thrakatulk, agh burzum-ishi krimpatul!
      Uzg-Microsoft-ishi amal fauthut burguuli.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://168673]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-05-27 21:31 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (98 votes). Check out past polls.