Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: help with error 'readline() on unopened filehandle'

by Plankton (Vicar)
on Feb 03, 2013 at 07:42 UTC ( [id://1016796]=note: print w/replies, xml ) Need Help??


in reply to help with error 'readline() on unopened filehandle'

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> ) { ...

Replies are listed 'Best First'.
Re^2: help with error 'readline() on unopened filehandle'
by bhargavkanakiya (Initiate) on Feb 03, 2013 at 07:49 UTC
    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

      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
      You need to "open" your input file dummy

        i understood that and updated the code as

        ... open my $MYFILE , "<$filename" || "Can't open the output file!"; ...

        then the error changed to 'readline() on closed filehandle $MYFILE' even though i have not closed the file. this is the first code that i've written using perl and i am completely new to it. any help is appreciated. thanks.

        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-28 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found