#!/usr/bin/perl # ------------------------------------------------------------------------------------------------------ # TaoTeChing.pl V1.00 # ------------------------------------------------------------------------------------------------------ # Display a Specfied or Random Chapter of the Tao Te Ching # Written by Lao-tzu # Translated by S. Mitchell # ------------------------------------------------------------------------------------------------------ use strict; use warnings; use constant error => -1; use constant noerror => 0; use constant false => 0; use constant true => 1; use constant MinChapter => 1; use constant MaxChapter => 81; use constant Left => -1; use constant Right => 1; $| = true; # # Modules Used # use Data::Dumper; use File::Basename; use Tk; use Tk::Adjuster; use Tk::Frame; use Tk::Text; # # Who am I? # my $MyName = (fileparse($0, '\..*'))[0]; # # Globals # my $Chapter; my $mw = new MainWindow; my $tf; my $aj; my $bf; my $OutputText; my $LeftButton; my $RightButton; # # GetChapter # # Get the Text of the Specified Chapter # # Arguments: $Chapter # # Returns: @ChapterText # sub GetChapter { my ($Chapter) = shift; my @ChapterText; my $FoundChapterHeading = false; while() { if(/^\Q$Chapter\E$/) { $FoundChapterHeading = true; next; } if($FoundChapterHeading) { if(/^\d+$/) { last; } else { push(@ChapterText, $_); } } } seek(DATA, 0, 0); return(@ChapterText); } # # DisplayChapter # # Display Specified Chapter # # Arguments: $Chapter # # Returns: # sub DisplayChapter { my ($Chapter) = shift; my @ChapterText = GetChapter($Chapter); my $MaxLineLength = 0; foreach(@ChapterText) { if(length($_) > $MaxLineLength) { $MaxLineLength = length($_); } } my $GeometryString = ($MaxLineLength * 8) . 'x' . (scalar(@ChapterText) * 20); $mw->geometry($GeometryString); $mw->title(' ' x ((($MaxLineLength - 2) / 2) + 5) . (sprintf('%2d', $Chapter))); $OutputText->delete('1.0', 'end'); foreach my $Line (@ChapterText) { print(' ' x (($MaxLineLength - length($Line)) / 2) . $Line); } $OutputText->pack(qw/-side bottom -fill both -expand 1/); } # # ButtonHandler # # Process Button Presses # # Arguments: $ButtonType # # Returns: # sub ButtonHandler { my ($Type) = shift; $Chapter += $Type; if($Chapter < MinChapter) { $Chapter = MaxChapter; } elsif($Chapter > MaxChapter) { $Chapter = MinChapter; } DisplayChapter($Chapter); } # # Main # if($ARGV[0] and $ARGV[0] > 0 and $ARGV[0] < 82) { $Chapter = $ARGV[0]; } else { $Chapter = int(rand(81)) + 1; } $tf = $mw->Frame; # Top Frame; $aj = $mw->Adjuster(-widget => $tf, -side => 'bottom', -restore => true); $bf = $mw->Frame; # Bottom Frame $OutputText = $tf->Text ( -height => '1' , -width => '1' , ); $LeftButton = $bf->Button ( -command => [ \&ButtonHandler, Left ] , -height => '1' , -text => '<' , -width => '1' , )->pack ( -side => 'left' , -fill => 'both' , -expand => true ); $RightButton = $bf->Button ( -command => [ \&ButtonHandler, Right ] , -height => '1' , -text => '>' , -width => '1' , )->pack ( -side => 'left' , -fill => 'both' , -expand => true ); tie *STDOUT, ref $OutputText, $OutputText; $tf->pack(qw/-side top -fill both -expand 1/); $aj->pack(qw/-side top -fill y/); $bf->pack(qw/-side top -fill y/); $LeftButton->pack; $RightButton->pack; DisplayChapter($Chapter); MainLoop; __DATA__ 1 The tao that can be told is not the eternal Tao The name that can be named is not the eternal Name. The unnamable is the eternally real. Naming is the origin of all particular things. Free from desire, you realize the mystery. Caught in desire, you see only the manifestations. Yet mystery and manifestations arise from the same source. This source is called darkness. Darkness within darkness. The gateway to all understanding. 2 When people see some things as beautiful, other things become ugly. When people see some things as good, other things become bad. Being and non-being create each other. Difficult and easy support each other. Long and short define each other. High and low depend on each other. Before and after follow each other. Therefore the Master acts without doing anything and teaches without saying anything. Things arise and she lets them come; things disappear and she lets them go. She has but doesn't possess, acts but doesn't expect. When her work is done, she forgets it. That is why it lasts forever. 3 . . .