Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

readline() on closed filehandle

by Jeri (Scribe)
on Oct 07, 2011 at 20:39 UTC ( [id://930248]=perlquestion: print w/replies, xml ) Need Help??

Jeri has asked for the wisdom of the Perl Monks concerning the following question:

I get the error "readline() on closed filehandle $INFILE at Fileappendor.pl line 20."

This is a simple example of my problem. I can't seem to figure it out. Even if I don't ever close the filehandle, I still get the error. I feel like this is a conceptual newbie mistake. Can someone please address what I'm doing wrong? Thanks in advance.

#!usr/bin/perl5.8.8 use strict; use warnings; fileappendor(); sub fileappendor { my $fileno = 1; my $outfile = "proteintable"; open (my $OUTFILE,">", $outfile); while($fileno <= 152) { my $infile = $fileno."_FamDATA"; open (my $INFILE,"<", $infile); while(<$INFILE>) { print $OUTFILE $_; } close ($INFILE); $fileno++; } } #end of sub fileappendor

Replies are listed 'Best First'.
Re: readline() on closed filehandle
by toolic (Bishop) on Oct 07, 2011 at 21:02 UTC
    One way to get that warning message is if the open failed for your input file. Check if the open succeeded, for example, with autodie.
Re: readline() on closed filehandle
by ww (Archbishop) on Oct 07, 2011 at 23:59 UTC

    ... as toolic said... or, another way to check:

    open (my $OUTFILE,">", $outfile) or die "Could not open $OUTFILE, $!";

    And why does that example deal with your outfile? Because, it's a 'really good idea' (©) to check all your opens and closes!

      It might be better to die with the lowercase $outfile, though, or avoid such similar names for two different things.
        ... certainly the 2nd --
        " ...avoid such similar names for two different things." (emphasis added!)

        ++ choroba.

        But I'm not sure the first really cures anything. choroba's first comment got me experimenting with code minimally modified from the OP's ...

        #!/usr/bin/perl use strict; use warnings; use 5.012; # #930268 my $OUTFILE; my @outfile=("F:/_Perl_/pl_test/abcd.out", "dev/nul/xyz.out"); for my $outfile(@outfile) { # $OUTFILE is a glob; dereference using $$OUTFILE. Otherwise warni +ng is ambiguous:'not open GLOB(0x1628c24) using' open ($OUTFILE,">", $outfile) or warn "\t Could not open $$OUTFILE + using \$outfile ($outfile), $!"; say "\t \$outfile is now $outfile"; print $OUTFILE "I wuz here in vers 1\n" or warn "\t Problem is wit +h \$outfile: $outfile\n"; close ($OUTFILE); } say "\n\n" . "-" x10 . " 2nd vers using lc \$outfile in the 'die'\n"; my $OUTFILE2; my @outfile2 = ("F:/_Perl_/pl_test/abcd2.out", "dev/nul/pqrst.out"); for my $outfile2(@outfile2) { say "\t \$outfile2 is currently $outfile2"; open ($OUTFILE2,">", $outfile2) or warn "\t Could not open $outfil +e2 using \$outfile2, $!"; print $OUTFILE2 "vers 2 produced this line\n" or warn "\t Problem +is with \$outfile2: $outfile2\n"; close ($OUTFILE2); }

        The results don't seem to me to support a preference for which $var to 'die' (or, in my code, to 'warn'):

        F:\_Perl_\pl_test>F:\_Perl_\pl_test\930268.pl $outfile is now F:/_Perl_/pl_test/abcd.out Could not open *main::$OUTFILE using $outfile (dev/nul/xyz.out), + No such file or directory at F:\_Perl_\pl_test\930268.pl line 12. $outfile is now dev/nul/xyz.out print() on closed filehandle $OUTFILE at F:\_Perl_\pl_test\930268.pl l +ine 14. Problem is with $outfile: dev/nul/xyz.out ---------- 2nd vers using lc $outfile in the 'die' $outfile2 is currently F:/_Perl_/pl_test/abcd2.out $outfile2 is currently dev/nul/pqrst.out Could not open dev/nul/pqrst.out using $outfile2, No such file o +r directory at F:\_Perl_\pl_test\930268.pl line 25. print() on closed filehandle $OUTFILE2 at F:\_Perl_\pl_test\930268.pl +line 26. Problem is with $outfile2: dev/nul/pqrst.out 10/08/2011 09:06a 22 abcd.out # content as expected + from the code above 10/08/2011 09:06a 27 abcd2.out # " 2 File(s) 49 bytes

        Those with greater wisdom may add insight with their comments (I hope).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found