Perl中readline()未打开的文件句柄错误
readline () unopened file handle error in Perl
我无法修复代码中的错误。我正在尝试获取代码以读取输入文件并仅提取 []
之间的内容。但是,我得到的错误是 readline() on unopened filehandle
...我不确定我在这里为 while ()
文件句柄做错了什么。
#!/usr/bin/perl
use warnings;
my $file = '';
my $newfile = '';
open($newfile, '>', 'newmyosin.fasta') or die "Can't create file", $!;
open($file, '<', 'myosin.fasta') or die "Can't open file", $!;
while(<$file>) {
print;
chomp;
if ( $_ =~ /\[(.+)\]/ ) {
$file = ;
}
}
因此,例如:
这将是我输入文件的一部分的样子:
>gi|115527082|ref|NP_005954.3| myosin-1 [Homo sapiens]
>gi|226694176|sp|P12882.3|MYH1_HUMAN RecName: Full=Myosin-1; AltName: Full=Myosin heavy chain 1; AltName: Full=Myosin heavy chain 2x; Short=MyHC-2x; AltName: Full=Myosin heavy chain IIx/d; Short=MyHC-IIx/d; AltName: Full=Myosin heavy chain, skeletal muscle, adult 1 [Homo sapiens]
>gi|119610411|gb|EAW90005.1| hCG1986604, isoform CRA_b [Homo sapiens]
MSSDSEMAIFGEAAPFLRKSERERIEAQNKPFDAKTSVFVVDPKESFVKATVQSREGGKVTAKTEAGATVTVKDDQVFPM
NPPKYDKIEDMAMMTHLHEPAVLYNLKERYAAWMIYTYSGLFCVTVNPYKWLPVYNAEVVTAYRGKKRQEAPPHIFSISD
NAYQFMLTDRENQSILITGESGAGKTVNTKRVIQYFATIAVTGEKKKEEVTSGKMQGTLEDQIISANPLLEAFGNAKTVR
NDNSSRFGKFIRIHFGTTGKLASADIETYLLEKSRVTFQLKAERSYHIFYQIMSNKKPDLIEMLLITTNPYDYAFVSQGE
ITVPSIDDQEELMATDSAIEILGFTSDERVSIYKLTGAVMHYGNMKFKQKQREEQAEPDGTEVADKAAYLQNLNSADLLK
ALCYPRVKVGNEYVTKGQTVQQVYNAVGALAKAVYDKMFLWMVTRINQQLDTKQPRQYFIGVLDIAGFEIFDFNSLEQLC
INFTNEKLQQFFNHHMFVLEQEEYKKEGIEWTFIDFGMDLAACIELIEKPMGIFSILEEECMFPKATDTSFKNKLYEQHL
GKSNNFQKPKPAKGKPEAHFSLIHYAGTVDYNIAGWLDKNKDPLNETVVGLYQKSAMKTLALLFVGATGAEAEAGGGKKG
GKKKGSSFQTVSALFRENLNKLMTNLRSTHPHFVRCIIPNETKTPGAMEHELVLHQLRCNGVLEGIRICRKGFPSRILYA
DFKQRYKVLNASAIPEGQFIDSKKASEKLLGSIDIDHTQYKFGHTKVFFKAGLLGLLEEMRDEKLAQLITRTQAMCRGFL
ARVEYQKMVERRESIFCIQYNVRAFMNVKHWPWMKLYFKIKPLLKSAETEKEMANMKEEFEKTKEELAKTEAKRKELEEK
MVTLMQEKNDLQLQVQAEADSLADAEERCDQLIKTKIQLEAKIKEVTERAEDEEEINAELTAKKRKLEDECSELKKDIDD
LELTLAKVEKEKHATENKVKNLTEEMAGLDETIAKLTKEKKALQEAHQQTLDDLQAEEDKVNTLTKAKIKLEQQVDDLEG
SLEQEKKIRMDLERAKRKLEGDLKLAQESTMDIENDKQQLDEKLKKKEFEMSGLQSKIEDEQALGMQLQKKIKELQARIE
ELEEEIEAERASRAKAEKQRSDLSRELEEISERLEEAGGATSAQIEMNKKREAEFQKMRRDLEEATLQHEATAATLRKKH
ADSVAELGEQIDNLQRVKQKLEKEKSEMKMEIDDLASNMETVSKAKGNLEKMCRALEDQLSEIKTKEEEQQRLINDLTAQ
RARLQTESGEYSRQLDEKDTLVSQLSRGKQAFTQQIEELKRQLEEEIKAKSALAHALQSSRHDCDLLREQYEEEQEAKAE
除此之外,我想创建一个新文件 "newmyosin.fasta",它将在 header 中的括号内为该样本提取有机体名称(例如 [Homo sapiens]
。 Perl 代码用于从 myosin.fasta 文件中读取多个样本,从括号 []
中取出名称,然后写入新文件(例如 newmyosin.fasta ).
谢谢!
正如我在 中所说,您在读取文件的过程中将文件句柄重新分配给捕获组。由于您为输出打开了一个单独的文件,我假设您想将匹配的字符串打印到该文件。
话说回来,你的要求很模糊,你的样本输入看起来不准确,你也没有提供任何样本输出,但如果我正确理解你的意图,我认为这就是你想要的:
my $file = 'myosin.fasta';
my $tmp = "$file.tmp";
open(my $new, '>', $tmp) or die "Can't open $tmp: $!";
open(my $old, '<', $file) or die "Can't open $file: $!";
while (<$old>) {
if (/\[([^]]+)\]/) {
print $new "\n";
}
}
close($old);
close($new);
rename($file, "$file.bak");
rename($tmp, $file);
脚本 运行 之后 myosin.fasta 的内容:
Homo sapiens
Homo sapiens
Homo sapiens
当你这样做时:
$file = ;
您覆盖了您的文件句柄。然后你不能再从它读取。你会得到提到的错误。
您当然应该将匹配项保存在其他地方,例如:
my $match = ;
可能还会打印它:
print $newfile $match;
我无法修复代码中的错误。我正在尝试获取代码以读取输入文件并仅提取 []
之间的内容。但是,我得到的错误是 readline() on unopened filehandle
...我不确定我在这里为 while ()
文件句柄做错了什么。
#!/usr/bin/perl
use warnings;
my $file = '';
my $newfile = '';
open($newfile, '>', 'newmyosin.fasta') or die "Can't create file", $!;
open($file, '<', 'myosin.fasta') or die "Can't open file", $!;
while(<$file>) {
print;
chomp;
if ( $_ =~ /\[(.+)\]/ ) {
$file = ;
}
}
因此,例如:
这将是我输入文件的一部分的样子:
>gi|115527082|ref|NP_005954.3| myosin-1 [Homo sapiens]
>gi|226694176|sp|P12882.3|MYH1_HUMAN RecName: Full=Myosin-1; AltName: Full=Myosin heavy chain 1; AltName: Full=Myosin heavy chain 2x; Short=MyHC-2x; AltName: Full=Myosin heavy chain IIx/d; Short=MyHC-IIx/d; AltName: Full=Myosin heavy chain, skeletal muscle, adult 1 [Homo sapiens]
>gi|119610411|gb|EAW90005.1| hCG1986604, isoform CRA_b [Homo sapiens]
MSSDSEMAIFGEAAPFLRKSERERIEAQNKPFDAKTSVFVVDPKESFVKATVQSREGGKVTAKTEAGATVTVKDDQVFPM
NPPKYDKIEDMAMMTHLHEPAVLYNLKERYAAWMIYTYSGLFCVTVNPYKWLPVYNAEVVTAYRGKKRQEAPPHIFSISD
NA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KMCRALEDQLSEIKTKEEEQQRLINDLTAQ
RARLQTESGEYSRQLDEKDTLVSQLSRGKQAFTQQIEELKRQLEEEIKAKSALAHALQSSRHDCDLLREQYEEEQEAKAE
除此之外,我想创建一个新文件 "newmyosin.fasta",它将在 header 中的括号内为该样本提取有机体名称(例如 [Homo sapiens]
。 Perl 代码用于从 myosin.fasta 文件中读取多个样本,从括号 []
中取出名称,然后写入新文件(例如 newmyosin.fasta ).
谢谢!
正如我在
话说回来,你的要求很模糊,你的样本输入看起来不准确,你也没有提供任何样本输出,但如果我正确理解你的意图,我认为这就是你想要的:
my $file = 'myosin.fasta';
my $tmp = "$file.tmp";
open(my $new, '>', $tmp) or die "Can't open $tmp: $!";
open(my $old, '<', $file) or die "Can't open $file: $!";
while (<$old>) {
if (/\[([^]]+)\]/) {
print $new "\n";
}
}
close($old);
close($new);
rename($file, "$file.bak");
rename($tmp, $file);
脚本 运行 之后 myosin.fasta 的内容:
Homo sapiens
Homo sapiens
Homo sapiens
当你这样做时:
$file = ;
您覆盖了您的文件句柄。然后你不能再从它读取。你会得到提到的错误。
您当然应该将匹配项保存在其他地方,例如:
my $match = ;
可能还会打印它:
print $newfile $match;