Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Inline::Files unopened filehandle errors

by NateTut (Deacon)
on Oct 15, 2009 at 22:02 UTC ( #801444=perlquestion: print w/ replies, xml ) Need Help??
NateTut has asked for the wisdom of the Perl Monks concerning the following question:

I am getting the following errors from the code below:

C:\Src\pl>perl TTC.pl 33:[1] 33:[3] 33:[] print() on unopened filehandle PREVIOUSCHAPTERS at TTC.pl line 56. print() on unopened filehandle PREVIOUSCHAPTERS at TTC.pl line 66. $Chapter[2]


This is my first time using Inline::Files. It seems to work properly when called like: C:\Src\pl>perl TTC.pl next which also writes to an inline file. The documentation states that the files are opened for read/write, but PREVIOUSCHAPTERS seems to be read only or perhaps closed for some reason. Any help or suggestions are appreciated.
#!/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"; last; } } # # 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 Inline::Files unopened filehandle errors
Select or Download Code
Re: Inline::Files unopened filehandle errors (seek)
by tye (Cardinal) on Oct 16, 2009 at 05:40 UTC

    You should try moving the seek up above the first place where to try to write to PREVIOUSCHAPTERS (but still after the end of the loop where you read from PREVIOUSCHAPTERS). No, I don't know if that will fix the problem; but it will at least mean you code matches how the documentation says you should do things. :)

    - tye        

      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:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://801444]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-12-29 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls