Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

help with error 'readline() on unopened filehandle'

by bhargavkanakiya (Initiate)
on Feb 03, 2013 at 07:32 UTC ( #1016792=perlquestion: print w/ replies, xml ) Need Help??
bhargavkanakiya has asked for the wisdom of the Perl Monks concerning the following question:

i am trying to edit the contents of each file in one folder and trying to save in another copy in another folder the code is shown below. after executing code i am getting 'readline() on unopened filehandle perl at line 21' error. please help me.

use strict; use warnings; sub variable; my $indirname = $ARGV[0]; my $variable = 00001; my $outdirname = "F:/TTS_RA_work/convert/edited"; opendir ( DIR, $indirname ) || die "Error in opening dir $indirname\n" +; my @files = grep(/.lab/,readdir(DIR)); closedir(DIR); foreach my $filename( @files ){ my $file = "text_000".$variable."."."lab"; my $outfile = "$outdirname/"."$file"; open my $output , ">$outfile" || die "Can't open the output file!" +; print "file: $file\n"; while ( my $line = <$filename> ) { $line =~ s/^\s+//; $line =~ s/ +/ /g; $line =~ s/\s+$/\n/g; print $output $line; } close ($output); $variable++; }

Comment on help with error 'readline() on unopened filehandle'
Download Code
Re: help with error 'readline() on unopened filehandle'
by Plankton (Priest) on Feb 03, 2013 at 07:42 UTC
    You need to open the file $filename before you can read it. I suspect you wanted to do somethings like ...
    open( MYFILE, "<$filename") or die ... ... while ( my $line = <MYFILE> ) { ...
      Thanks! can you please look at the updated code and see where the error is? i saw that i made that mistake but still i am facing the same error
        You need to "open" your input file dummy

        Hi bhargavkanakiya,
        from your code:

        foreach my $filename( @files ){ my $file = "text_000".$variable."."."lab"; my $outfile = "$outdirname/"."$file"; open my $output , ">$outfile" || die "Can't open the output file!" +; print "file: $file\n"; while ( my $line = <$filename> ) { $line =~ s/^\s+//; $line =~ s/ +/ /g; $line =~ s/\s+$/\n/g; print $output $line; } close ($output); $variable++; }
        $filename is the name of the file to open, and read from. In your code, "$filename" is not a FILEHANDLE.
        In Perl Programming, you need a file-handle in the open function like thus:  open FILEHANDLE,MODE,EXPR
        Note: that you are not opening, your file named "$filename" to read from. You are just using it in the while loop with readline function as while ( my $line = <$filename> ) {...} which should really be while ( my $line = <FILEHANDLE> ) {...} So, your code should be something like:
        foreach my $filename( @files ){ my $file = "text_000".$variable."."."lab"; my $outfile = "$outdirname/"."$file"; open my $output,'>',$outfile or die "Can't open the output file: $ +!"; ## updated open my $fh,'<',$filename or die "can't open file: $!"; ## add +ed print "file: $file\n"; while ( my $line = <$fh> ) { $line =~ s/^\s+//; $line =~ s/ +/ /g; $line =~ s/\s+$/\n/g; print $output $line; } close $fh or die "can't close file: $!"; ## added close $output or die "can't close file: $!"; ## updated $variable++; }
        NOTE: All codes are not tested please.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: help with error 'readline() on unopened filehandle'
by kcott (Abbot) on Feb 03, 2013 at 07:46 UTC

    I would have thought the error message was fairly self-explanatory. See open.

    -- Ken

Re: help with error 'readline() on unopened filehandle'
by Anonymous Monk on Feb 03, 2013 at 09:24 UTC

    I'm not sure whether the answer is in the above rather lengthy-looking thread, but it is:

    # the die() will never get executed # because of operator precedence open my $output , ">$outfile" || die "Can't open the output file!"; # this would work: open(my $output , ">$outfile") || die "Can't open the output file!"; # and so would this: open my $output , ">$outfile" or die "Can't open the output file!";
Re: help with error 'readline() on unopened filehandle'
by tmharish (Friar) on Feb 03, 2013 at 12:24 UTC

    There are two different answers above which point you to this but the essence is - If you are not using brackets you must know operator precedence.

    And of course you should also know that 'or' works between statements because each statement evaluates to true or false.

    Getting messed up with file open occurs so often that I find it far easier to enforce die on failure. I understand why Perl does this but personally when I open a file, nearly always, I expect to have it open.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-09-30 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (369 votes), past polls