tao.pl

Here is the source code:


#!/usr/bin/perl

# tao.pl - fetch verse from www.taoteching.org
#
# a number (1-81) of a specific chapter could be given.
# if not given, a random one will be selected.
#
#
# This script uses:
#
# subroutines / functions
# split
# foreach
# command line arguments
# regular expression
# substitution
# LWP module

use LWP::Simple;

# a utility to remove the linebreak tag
# from a string. sub clean_the_breaks { $s = shift; $s =~ s/<br />//; return $s; } # fetch a page from the website. Take a number 1-81 as argument. sub tao { $num = shift; print "Chapter $num:\n\n"; $page = get("http://www.taoteching.org/chapters/$num.htm"); @lines = split("\n",$page); $state = 0; foreach $P (@lines) { if ($P =~ /<\/td>/ && $state == 1) { $state = 0; } if ($state == 1) { print clean_the_breaks($P)."\n"; } if ($P =~ /class="versecopy"/) { $state = 1; } } } # choose a random page. return number 1-81. sub r_tao { tao(int(rand(80) + 1)); } # Was a command line argument given? if so, fetch that # number (1-81) $num_args = $#ARGV + 1; if ($num_args == 1) { $arg = int($ARGV[0]); if ($arg > 0 && $arg < 82) { tao($arg); } else { r_tao(); } } else { r_tao(); }