<?xml version="1.0" encoding="windows-1252"?>
<node id="161891" title="Jargon file of the day" created="2002-04-25 04:34:30" updated="2005-07-27 09:06:44">
<type id="1980">
snippet</type>
<author id="141348">
Dog and Pony</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
Small CGI program to return a random entry from [http://www.tuxedo.org/~esr/jargon/html/|the jargon file].
&lt;p&gt;
I started thinking about how I really should read stuff on that place more, so I whipped this together and am using it as my start page. You can currently test it at [http://dogandpony.perlmonk.org/cgi-bin/jargon.pl].&lt;/p&gt;</field>
<field name="snippetcode">
&lt;CODE&gt;
#!/usr/bin/perl -w

# Random jargon file redirect.
# 
# Use it as your start page, or
# as yet another funny link on
# your home page...
#
# Stuff to do includes error handling
# and possibly local mirroring.
# But hey, it's a snippet, not production code... :)

use strict;
use LWP::Simple;
use HTML::TokeParser;
use CGI;

# Everybodys fav object.
my $q = CGI-&gt;new;

# The chapters to choose from.
my @chapters = (0, 'A'..'Z');

# Choose one.
my $chosen = $chapters[ rand(@chapters) ];

# Get the index page for choen chapter:
my $index_page = get( "http://www.tuxedo.org/~esr/jargon/html/-$chosen-.html" );

# Another fav object:
my $parser = HTML::TokeParser-&gt;new( \$index_page );	

# List of pages under chosen chapter:
my @href_list;

# This is based on the current format of the jargon files
# which should be unlikely to change though.
while( $parser-&gt;get_tag( 'li' ) )
{
	# Get all links on that particular page.
	push @href_list, @{$parser-&gt;get_tag( 'a' )}[1]-&gt;{'href'};
}

# And randomly choose one...
my $href = $href_list[ rand(@href_list) ];

# ... which we redirect to.
print $q-&gt;redirect( "http://www.tuxedo.org/~esr/jargon/html/$href" );
&lt;/CODE&gt;</field>
</data>
</node>
