2013-02-22 76 views
3

运行此代码会生成一个错误,指出在第14行关闭的文件句柄SEQFILE上的“readline()”。之前的搜索都评论了在打开后应如何放置某种条件。这样做只会杀死程序(我离开它,所以我可以看到它为什么没有打开)。我猜想更深层次的问题是为什么它不打开我的文件?关闭的文件句柄上的readline()

#!/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; 

回答

4

要知道为什么open失败,改变这种:

open (SEQFILE, $seq); 

这样:你在哪里运行之间

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

(见the perlopentut manpage

+0

三个参数的open是更好:) – squiguy 2013-02-22 00:27:30

+1

它说:“没有这样的文件第11行,行 1”,但我知道这个文件是存在的! – Citizin 2013-02-22 00:34:48

+0

@Citizin:打印的$ seq'的值是多少? – ruakh 2013-02-22 00:35:39

0

还要注意的是,如果你使用||而不是像这样的“或”:

打开SEQFILE,$ seq ||死“无法打开$ seq':$!”;

这将无法正常工作。见链接:

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

+0

-1,对不起。您误解了您链接到的博客文章。 'open(SEQFILE,$ seq)||死“无法打开'$ seq':$!”;'实际上* does *正常工作,因为当函数名后的第一个标记(在本例中为'open')是'(',Perl解释' ('介绍参数列表(顺便说一句,它并不总是你想要的 - 这意味着'print(3 + 4)/ 2'相当于'(print 7)/ 2',而不是' print 3.5'。因此,如果你启用了警告,Perl会在你的函数名和'('。)之间有空格时警告你。 – ruakh 2015-02-02 06:19:33

相关问题