Fortune(3pm)
π Fortune(3pm) β the vibe-y guide π
okay bestie, we're doing Fortune, a Perl module that lives in User Contributed Perl Documentation, section 3pm, and it's literally here to read and write your fortune (strfile) databases. no cap. let's go π
β¨ NAME β¨
Fortune β read and write fortune (strfile) databases π
π¦ SYNOPSIS π¦
the input era π:
# input
$ffile = new Fortune ($base_filename);
$ffile->read_header ();
$num_fortunes = $ffile->num_fortunes ();
$fortune = $ffile->read_fortune ($num);
$fortune = $ffile->get_random_fortune ();
making a header file out of a data file β NOT IMPLEMENTED YET π:
# create header file from data file -- NOT IMPLEMENTED YET
$ffile = new Fortune ($base_filename);
$ffile->write_header ();
writing to the data file β also NOT IMPLEMENTED YET π:
# write to data file -- NOT IMPLEMENTED YET
$ffile = new Fortune (">>$base_filename");
$ffile->write_fortune ($fortune);
π DESCRIPTION π
so the fortune program is this small but iconic little slice of Unix culture, and this module's whole mission is to bring support for its "fortune cookie" databases to Perl girlies everywhere. for efficiency reasons, every version of fortune leans on a binary header that's mostly just offsets pointing into the actual fortune file. modern versions of fortune keep that header in a separate file, and that's the style Fortune adopted. the older style β munging the header and the data together into one big "compiled" file β is not (currently) supported, periodt.
using Fortune makes writing a simplified version of the fortune program genuinely trivial, like:
# 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;
and it can be compressed considerably β¨:
print new Fortune ($ARGV[0])->read_header()->get_random_fortune();
obviously this does not serve all of fortune's interesting features β parallel databases of offensive fortunes? selection of long or short fortunes? dealing with multiple fortune files? none of that here, bestie. if you want fortune, use it. but if you just want a simple Perl interface to its data files, Fortune is your main character π
currently, the Fortune module does not support writing fortune databases. if it did, writing a simplified strfile (that's the program that processes a fortune database and makes 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 ();
quick note though π β the header filename is assumed to be just the name of the main fortune database with .dat appended. you can hand an alternate header filename to the constructor, new(), if that's more your vibe.
πΈ METHODS πΈ
π Initialization/cleanup π
new (FILE [, HEADER_FILE])β opens a fortune cookie database, slay.FILEis the name of the data file you're opening, andHEADER_FILE(if you give it) is the name of the header file that contains (or will contain) the meta-data about the fortune database. ifHEADER_FILEisn't given, it defaults toFILEwith.datappended.
the data file gets opened via open_file(), which dies 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, and we're not about to embarrass ourselves. instead, you must explicitly call read_header() or write_header() as appropriate. do not skip this step fr fr.
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 absolutely nothing otherwise. no notes.
π§ Header functions (read and write) π§
read_header ()β reads the header file associated with this fortune database. the name of the header file is determined by the constructornew: either it's based on the name of the data file, or it was supplied by the caller.
if the header file doesn't exist, this function calls compute_header() automatically, which has the same effect as reading the header from a file. cute, right?
the header contains these values, stored as attributes of the Fortune object:
versionβ version numbernumstrβ number of strings (fortunes) in the filemax_lengthβ length of longest string in the filemin_lengthβ length of shortest string in the fileflagsβ bit field for flags (see strfile(1) man page)delimβ character that delimits fortunes
numstr is available via the num_fortunes() method; if you're interested in the others, you'll have to go grubbing through the Fortune object, like:
$fortune_file = new Fortune ('fortunes');
$fortune_file->read_header ();
$delim = $fortune_file->{'delim'};
read_header() dies if there are any problems reading the header file, e.g. if it seems to be corrupt or truncated. no saving you there bestie π
read_header() returns the current Fortune object, which allows for sneaky one-liners (peep the examples above π).
compute_header ([DELIM])β reads the contents of the fortune file and computes the header information that would normally be found in a header (.dat) file. super useful if you maintain a file of fortunes by hand and don't have the corresponding data file.
an optional delimiter argument may be passed to this function; if present, that delimiter gets used to separate entries in the fortune file. if not provided, the existing delim attribute of the Fortune object is used. and if that is not defined, then a percent sign (%) is used. iconic fallback behavior honestly.
num_fortunes ()β returns the number of fortunes found byread_header().write_header ([DELIM])β is not yet implemented π
π¦ Fortune input π¦
get_fortune (NUM)β reads string number NUM from the open fortune file.NUMis zero-based, ie. it must be between0andnum_fortunes()-1(inclusive).croaks if you haven't opened the file and read the header, or ifNUMis 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 withread_header(). that part is on you.) returns the text of the fortune as a (possibly) multiline string πget_random_fortune ()β picks a random fortune for you and reads it withread_fortune(). it's giving chaos, it's giving destiny π²
π«Ά Fortune output π«Ά
write_fortune (FORTUNE)β is not yet implemented π
π AUTHOR AND COPYRIGHT π
written by Greg Ward <gward@python.net>, 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 Fortune module from my web page:
http://starship.python.net/~gward/perl/
and it can also be found on CPAN π«
if you're using an operating system that's lacking a sufficient sense of humour to include fortune as part of its standard installation (most commercial Unices seem to be so afflicted π), the Linux world has a solution: the fortune-mod distribution. the latest version as of this writing is fortune-mod-9708, and the README file says you can find it at
http://www.progsoc.uts.edu.au/~dbugger/hacks/hacks.html
this is the fortune implementation on which the Fortune module is based. π«‘
and that's the whole page, bestie π this was Fortune(3pm) from perl v5.36.0, dated 2022-11-19 β go read some fortunes, you ate π
β¨
π¦ where this page comes from
- debian bookworm: libfortune-perl 0.2+ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libfortune-perl 0.2+ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libfortune-perl 0.2+ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libfortune-perl 0.2+ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libfortune-perl 0.2+ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms