SYNOPSIS

   # input
   $ffile = new Fortune ($base_filename);
   $ffile->read_header ();
   $num_fortunes = $ffile->num_fortunes ();
   $fortune = $ffile->read_fortune ($num);
   $fortune = $ffile->get_random_fortune ();

# create header file from data file -- NOT IMPLEMENTED YET $ffile = new Fortune ($base_filename); $ffile->write_header ();

# write to data file -- NOT IMPLEMENTED YET $ffile = new Fortune (">>$base_filename"); $ffile->write_fortune ($fortune);

DESCRIPTION

The \*(C`fortune\*(C' program is a small but important part of the Unix culture, and this module aims to provide support for its \*(L"fortune cookie\*(R" databases to Perl programmers. For efficiency, all versions of \*(C`fortune\*(C' rely on a binary header consisting mainly of offsets into the fortune file proper. Modern versions of fortune keep this header in a separate file, and this is the style adopted by the \*(C`Fortune\*(C' module; the older style of munging the header and data into one large \*(L"compiled\*(R" file is not (currently) supported.

Using the \*(C`Fortune\*(C' module makes it trivial to write a simplified version of the \*(C`fortune\*(C' program:

# trivial 'fortune' progam my $fortune_filename = $ARGV[0]; my $fortune_file = new Fortune ($fortune_filename); $fortune_file->read_header (); my $fortune = $fortune_file->get_random_fortune (); print $fortune;

This can be compressed considerably:

print new Fortune ($ARGV[0])->read_header()->get_random_fortune();

Of course, this doesn't provide all of \*(C`fortune\*(C''s interesting features, such as parallel databases of offensive fortunes, selection of long or short fortunes, dealing with multiple fortune files, etc. If you want \*(C`fortune\*(C', use it \*(-- but if you just want a simple Perl interface to its data files, the \*(C`Fortune\*(C' module is for you.

Currently, the \*(C`Fortune\*(C' module does not support writing fortune databases. If it did, writing a simplified \*(C`strfile\*(C' (the program that processes a fortune database to create the header file) would also be trivial:

# trivial (and hypothetical) 'strfile' program my $fortune_filename = @ARGV[0]; my $fortune_file = new Fortune ($fortune_filename); $fortune_file->write_header ();

Note that the header filename is assumed to be just the name of the main fortune database, with ".dat" appended. You can supply an alternate header filename to the constructor, \*(C`new()\*(C', if you wish.

METHODS

\$1

new (\s-1FILE\s0 [, \s-1HEADER_FILE\s0])

Opens a fortune cookie database. \s-1FILE\s0 is the name of the data file to open, and \s-1HEADER_FILE\s0 (if given) the name of the header file that contains (or will contain) meta-data about the fortune database. If \s-1HEADER_FILE\s0 is not given, it defaults to \s-1FILE\s0 with ".dat" appended. The data file is opened via \*(C`open_file()\*(C', which \*(C`die\*(C's if the file cannot be opened. The header file is not opened, whether you supply its filename or not \*(-- after all, it might not exist yet. Rather, you must explicitly call \*(C`read_header()\*(C' or \*(C`write_header()\*(C' as appropriate.

open_file ()

Opens the fortune file whose name was supplied to the constructor. Dies on failure.

close_file ()

Closes the fortune file if it's open; does nothing otherwise.

read_header ()

Reads the header file associated with this fortune database. The name of the header file is determined by the constructor \*(C`new\*(C': either it is based on the name of the data file, or supplied by the caller. If the header file does not exist, this function calls \*(C`compute_header()\*(C' automatically, which has the same effect as reading the header from a file. The header contains the following values, which are stored as attributes of the \*(C`Fortune\*(C' object:

version number number of strings (fortunes) in the file length of longest string in the file length of shortest string in the file bit field for flags (see strfile\|(1) man page) character that delimits fortunes

\*(C`numstr\*(C' is available via the \*(C`num_fortunes()\*(C' method; if you're interested in the others, you'll have to go grubbing through the \*(C`Fortune\*(C' object, e.g.: $fortune_file = new Fortune ('fortunes'); $fortune_file->read_header (); $delim = $fortune_file->{'delim'}; \*(C`read_header()\*(C' \*(C`die\*(C's if there are any problems reading the header file, e.g. if it seems to be corrupt or truncated. \*(C`read_header()\*(C' returns the current \*(C`Fortune\*(C' object, to allow for sneaky one-liners (see the examples above).

compute_header ([\s-1DELIM\s0])

Reads the contents of the fortune file and computes the header information that would normally be found in a header (.dat) file. This is useful if you maintain a file of fortunes by hand and do not have the corresponding data file. An optional delimiter argument may be passed to this function; if present, that delimiter will be used to separate entries in the fortune file. If not provided, the existing \*(C`delim\*(C' attribute of the Fortune object will be used. If that is not defined, then a percent sign (\*(L"%\*(R") will be used.

num_fortunes ()

Returns the number of fortunes found by \*(C`read_header()\*(C'.

write_header ([\s-1DELIM\s0])

is not yet implemented.

get_fortune (\s-1NUM\s0)

Reads string number \s-1NUM\s0 from the open fortune file. \s-1NUM\s0 is zero-based, ie. it must be between 0 and \*(C`num_fortunes()-1\*(C' (inclusive). \*(C`croak\*(C's if you haven't opened the file and read the header, or if \s-1NUM\s0 is out of range. (Opening the file is pretty hard to screw up, since it's taken care of for you by the constructor, but you have to read the header explicitly with \*(C`read_header()\*(C'.) Returns the text of the fortune as a (possibly) multiline string.

get_random_fortune ()

Picks a random fortune for you and reads it with \*(C`read_fortune()\*(C'.

write_fortune (\s-1FORTUNE\s0)

is not yet implemented.

AUTHOR AND COPYRIGHT

Written by Greg Ward <[email protected]>, 20 February 1999.

Copyright (c) 1999-2000 Gregory P. Ward. All rights reserved. This is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AVAILABILITY

You can download the \*(C`Fortune\*(C' module from my web page:

http://starship.python.net/~gward/perl/

and it can also be found on \s-1CPAN\s0.

If you are using an operating system lacking a sufficient sense of humour to include \*(C`fortune\*(C' as part of its standard installation (most commercial Unices seem to be so afflicted), the Linux world has a solution: the \*(C`fortune-mod\*(C' distribution. The latest version as of this writing is \*(C`fortune-mod-9708\*(C', and the \s-1README\s0 file says you can find it at

http://www.progsoc.uts.edu.au/~dbugger/hacks/hacks.html

This is the \*(C`fortune\*(C' implementation on which the \*(C`Fortune\*(C' module is based.