2013-02-22 72 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

相關問題