summaryrefslogtreecommitdiff
path: root/lib/rand.pm
diff options
context:
space:
mode:
authorJules Laplace <carbon@melanarchy.org>2013-08-02 17:14:41 -0500
committerJules Laplace <carbon@melanarchy.org>2013-08-02 17:14:41 -0500
commite9192b3d42660a5781101df4357d276318151e8a (patch)
tree059eb6ace6147cf9559af74ed1ab5e221c80e280 /lib/rand.pm
parent79670053c7247d3a49b607960efd284e93f057e5 (diff)
cgi-bin & lib
Diffstat (limited to 'lib/rand.pm')
-rw-r--r--lib/rand.pm20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/rand.pm b/lib/rand.pm
new file mode 100644
index 0000000..bf42e7d
--- /dev/null
+++ b/lib/rand.pm
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+
+sub get_random_line
+ {
+ my ($file) = @_;
+ my $line;
+
+ # inscrutable random line algorithm -- from the camel book
+ # $. is the number of the last line accessed
+ open FORTUNES, "$BUCKY_FORTUNES/$file" or error("Could not access fortune $file");
+ srand;
+ rand($.) < 1 && ($line = $_) while <FORTUNES>;
+ close FORTUNES;
+
+ chomp $line;
+ return $line;
+ }
+
+1;
+