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

Hello monks,

I am confronted with an error here I am not understanding for I have done this many time and this time it does not seem to work.
I looked at earlier code I made in Perl and it is pretty much done in a similar manner.
Any clues on why I have this error: readline() on close filehandle $handle?

Thanks

sub changeData{ my ($self) = @_; `$toolPath/an2k2txt ./Templates/DFP.nst ./tmp/Data2`; open my $handle, "<", "./Data2"; my $file = new IO::File("./$self->{_nistName}.tmp","w"); my $data; while(<$handle>){ if ($_ =~ m/$self->{_field}/i){ chomp($_); $_ =~ s/=.*//; #Seems that \x1F\x0A is the delimiter at the e +nd of the lines $data = $_."=".$self->{_newValue}."\x1F\x0A"; print $file $data; } else{ print $file $_; } print "$_\n"; } close $handle; `$toolPath/txt2an2k ./$self->{_nistName}.tmp ./Nist/$self->{_n +istName}.bad`; system("rm -f ./*.tmp"); }

Replies are listed 'Best First'.
Re: readline() on close filehandle $handle
by Corion (Patriarch) on Jun 05, 2008 at 10:06 UTC

    You don't tell us the full error message. Perl tells you the line number where the error or warning occurs, and that line number helps us to find the line where the error occurs.

    The warning most likely is issued because you are not checking whether opening the file works:

    open my $handle, "<", "./Data2" or die "Couldn't open ./Data2";

    Most likely, you mean ./tmp/Data2 and not ./Data2, but Perl will tell you.

Re: readline() on close filehandle $handle
by moritz (Cardinal) on Jun 05, 2008 at 10:10 UTC
    `$toolPath/an2k2txt ./Templates/DFP.nst ./tmp/Data2`;
    `...` execute the enclosed command, and return the output of the command. If you don't need the output, use system instead.
Re: readline() on close filehandle $handle
by andreas1234567 (Vicar) on Jun 05, 2008 at 10:55 UTC
    Use the helpful advice above from Corion above or prepend use Fatal qw(open close); to your code.
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]