4

Running this code produces an error stating "readline() on closed filehandle SEQFILE at line 14." Previous searches have all commented on how one should put some type of conditional after open. Doing so just kills the program (i left it out so I could see why it didn't open). I would guess the deeper problem is why is it not opening my file?

#!/usr/bin/perl -w

#Ask user to point to file location and collect from the keyboard
print "Please specify the file location: \n";
$seq = <STDIN>;

#Remove the newline from the filename
chomp $seq;

#open the file or exit
open (SEQFILE, $seq);

#read the dna sequence from the file and store it into the array variable @seq1
@seq1 = <SEQFILE>;

#Close the file
close SEQFILE;

#Put the sequence into a single string as it is easier to search for the motif
$seq1 = join( '', @seq1);

#Remove whitespace
$seq1 =~s/\s//g;

#Use regex to say "Find 3 nucelotides and match at least 6 times
my $regex = qr/( ([ACGT]{3}) \2{6,} )/x;
$seq1 =~ $regex;
printf "MATCHED %s exactly %d times\n", $2, length($1)/3;
exit;
Citizin
  • 87
  • 1
  • 1
  • 7

3 Answers3

5

To see why open is failing, change this:

open (SEQFILE, $seq);

to this:

open (SEQFILE, $seq) or die "Can't open '$seq': $!";

(See the perlopentut manpage.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

Also note that if you use a || instead of an "or" in the line like so:

open SEQFILE, $seq || die "Can't open '$seq': $!";

This will not work correctly. See Link:

https://vinaychilakamarri.wordpress.com/2008/07/27/subtle-things-in-perl-part-2-the-difference-between-or-and/

  • -1, sorry. You have misunderstood the blog post that you link to. `open (SEQFILE, $seq) || die "Can't open '$seq': $!";` actually *does* work correctly, because when the first token after a function-name (in this case `open`) is `(`, Perl interprets the `(` as introducing the argument-list. (Which, incidentally, is not always what you want -- it means that `print (3 + 4) / 2` is equivalent to `(print 7) / 2` rather than to `print 3.5`. For this reason, if you enable warnings, Perl will warn you if you have a space between the function-name and the `(`.) – ruakh Feb 02 '15 at 06:19
-1

Mismatch between where you are running your program from and where the 'file to open' is stored.

this is where i found it, Perl Readline on closed filehandle - file does not exist error

Community
  • 1
  • 1
Arun Joshi
  • 47
  • 1
  • 4