Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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 rifling through the Monastery: (6)
As of 2014-10-02 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (54 votes), past polls