CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help solving an error" readline() on closed filehandle"

 



maneshmotts
Novice

Sep 26, 2012, 11:50 PM

Post #1 of 4 (1400 views)
Help solving an error" readline() on closed filehandle" Can't Post

HI ,

I using a file handler to copy it into an array, but I am getting an
error.readline() on closed filehandle FILE


Code
#!usr/bin/perl 

use strict;
use warnings;

open(FILE,"<input.txt");
my @number;

while(<FILE>){
@number = <FILE>;
}
print @number;

close(FILE);



Quote


Quote


Quote



FishMonger
Veteran / Moderator

Sep 27, 2012, 6:15 AM

Post #2 of 4 (1377 views)
Re: [maneshmotts] Help solving an error" readline() on closed filehandle" [In reply to] Can't Post

You're receiving that error because the open call failed.

You need to add error handling on the open call to verify that it was successful. I would also suggest that you use a lexical var for the filehandle and the 3 arg form of open.


Code
my $file = 'input.txt'; 
open my $fh, '<', $file or die "failed to open '$file' <$!>";



maneshmotts
Novice

Oct 15, 2012, 12:14 AM

Post #3 of 4 (1027 views)
Re: [FishMonger] Help solving an error" readline() on closed filehandle" [In reply to] Can't Post

HI thanks for the reply could you please explain me about the three variable form .

The $fh variable you have use here is consider as an file handler ?

Then can I use the as "my @array = <fh>;" will this work please let me know

Thanks


Laurent_R
Veteran / Moderator

Oct 15, 2012, 4:36 AM

Post #4 of 4 (1023 views)
Re: [maneshmotts] Help solving an error" readline() on closed filehandle" [In reply to] Can't Post

Hi

the three arguments form of the open command:
- First arg: file handle
- second arg: open mode
- third arg: file name.

Yes, $fh is a file handle and you can use it as follows:

while (my $line = <$fh>) { # ...
or
my @lines = <$fh>;
#...

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives