Thank You. Now I only get one error message:
C:\Src\pl>perl TTC.pl
33:[1]
33:[3]
33:[]
print() on unopened filehandle PREVIOUSCHAPTERS at TTC.pl line 56.
$Chapter[4]
Here is the revised code:
#!/usr/bin/perl
use strict;
use warnings;
use constant MinChapter => 1;
use constant MaxChapter => 4;
$| = 1;
use File::Basename;
use Inline::Files -backup;
my $MyName = (fileparse($0, '\..*'))[0];
my $Chapter;
sub PickARandomChapter
{
my $Chapter;
#
# Get an Initial Random Chapter
#
$Chapter = int(rand(MaxChapter)) + MinChapter;
#
# Read in Previously Viewed Chapters
#
my $fh;
my %PreviousChapters;
while(<PREVIOUSCHAPTERS>)
{
chomp();
print(__LINE__ . ":\[$_\]\n");
if($_ ne '')
{
$PreviousChapters{$_} = 0;
}
}
my $TriedChapters = 0;
while($TriedChapters < MaxChapter)
{
if(exists($PreviousChapters{$Chapter}))
{
$TriedChapters++;
$Chapter++;
if($Chapter > MaxChapter)
{
$Chapter = MinChapter;
}
}
else
{
$PreviousChapters{$Chapter} = 1;
print PREVIOUSCHAPTERS "$Chapter\n";
return($Chapter);
}
}
#
# All Chapters have been read
# Start Over
#
seek(PREVIOUSCHAPTERS, 0, 0);
print PREVIOUSCHAPTERS "$Chapter\n";
return($Chapter);
}
if($ARGV[0])
{
if(lc($ARGV[0]) eq 'next')
{
$Chapter = <LASTCHAPTER>;
print("\$Chapter\[$Chapter\]\n");
$Chapter >= MaxChapter ? $Chapter = MinChapter : $Chapter++;
print("\$Chapter\[$Chapter\]\n");
seek LASTCHAPTER, 0, 0;
print LASTCHAPTER $Chapter . "\n";
close LASTCHAPTER;
}
elsif(($ARGV[0] =~ /\d+/) and $ARGV[0] > 0 and $ARGV[0] < 82)
{
$Chapter = $ARGV[0];
}
else
{
print STDERR << "USAGE";
Usage: $MyName [ChapterNumber] [Next]
$MyName - Prints a Random Chapter from the Tao Te Ching
$MyName ChapterNumber - Prints ChapterNumber Chapter from the Tao Te C
+hing
$MyName Next - Prints the Next Chapter from the Tao Te Ching
+Starting at Chapter 1
USAGE
exit(1);
}
}
else
{
$Chapter = PickARandomChapter();
}
print("\$Chapter\[$Chapter\]\n");
__LASTCHAPTER__
1
__PREVIOUSCHAPTERS__
1
3
__CHAPTERS__
1
foo
foo bar
foo bar baz
2
foo
foo bar
3
foo
foo bar baz
4
foo
foo bar
foo bar baz
foo
foo bar
foo bar baz
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|