Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Debugging for realine() on closed filehandle

Status
Not open for further replies.

szzxy

Technical User
Aug 14, 2010
8
0
0
CA
Dear Programmers,
Please help me debug the following perl script. Thanks in advance!
I keep getting the same error msg "readline() on closed filehandle file1 at drug_list_formidterm.pl line 12."


#!usr/local/bin/perl -w
use strict;

my @table;
my $drug;
my $category;
my $line;

open (file1, $ARGV[0]);
open (file2, ">tab_delimited.txt");

while (<file1>){

if ($_=~/^(.+)\t(.+)$/){

$drug=$1;
$category=$2;
$line=$drug."\t".$category;
}
push (@table, $line);





}
foreach my $seq(@table){
print file2 "$seq.\n";
}

close (file1);
close (file2);


Regards,
novice for perl

 
I would guess that your first open() statement failed. So your <file1> statement in the while-loop (at line 12) cannot read the unopened file.

What is in $ARGV[0]? Make sure it contains a proper file name (preferably fully qualified with a path) and is accessible with read permission.

Also, try hard-coding different file names in the open() to see what happens.
 
For starters:

$fileName = $ARGV[0] || die "No file name provided.\n";
chomp ($fileName);
print "$fileName does not exist" and die unless (-e $fileName);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top