Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Anonymous Monk
on Feb 03, 2013 at 07:59 UTC ( #1016799=note: print w/ replies, xml ) Need Help??

Comment on Re^3: help with error 'readline() on unopened filehandle'
Re^4: help with error 'readline() on unopened filehandle'
by bhargavkanakiya (Initiate) on Feb 03, 2013 at 08:10 UTC

    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.

      Don't put a '$' on your file handle. Here's how you open and read from a file:
      $ cat file.txt line one line two line three $ cat open_and_read.pl #!/usr/bin/perl -w use strict; # usually one does not put a $ on a filehandle variable open ( MYFILE, "<file.txt" ) or die "could not be opening the file now + :$!\n"; while ( my $line = <MYFILE> ) { print $line; } close ( MYFILE ); $ ./open_and_read.pl line one line two line three
        Also, 3 argument open is safer than 2 argument open -- sure you hardcoded the filename, but the OP hasn't

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2015-07-05 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls