Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Inline::Files unopened filehandle errors (seek)

by NateTut (Deacon)
on Oct 16, 2009 at 15:17 UTC ( #801576=note: print w/ replies, xml ) Need Help??


in reply to Re: Inline::Files unopened filehandle errors (seek)
in thread Inline::Files unopened filehandle errors

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


Comment on Re^2: Inline::Files unopened filehandle errors (seek)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://801576]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-10-02 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls