【问题标题】:Problems with 'my' [closed]“我的”的问题[关闭]
【发布时间】:2016-07-06 23:17:17
【问题描述】:

我有一个程序旨在从 multifasta 中提取行并通过 THREADER 运行它们:

#¡/usr/bin/perl
use warnings;
use strict;
use File::Temp qw(tempfile);

my $filename = 'unchar_prot';   #open file
open (my $fh, '<:encoding(UTF-8)', $filename)
    or die "Error - cannot open file";

my %id2seq = (); #create a hash
my $seq_id = '';
while (my $line = <$fh>) {
    chomp $line;
    if ($line =~ /^>(.+)/) { #Find lines starting in >
        $seq_id = $1;
    } else {
        $id2seq{$seq_id} .= $line; #Store seq and ID on hash
    }
}

open(my $outfile, '>', 'out.txt') or die
close (my $outfile)

while (my ($seq_id, $seq) = each %id2seq) { #Call key and value from hash 
    my ($temp_fh, $prot) = tempfile ("tempXXXX", SUFFIX => '.seq'); #create a temporary file
    print $temp_fh ">$seq_id\n$seq\n"; #print the 2 lines to the temp file 
    my ($out_fh, $out) = tempfile("outXXXX", SUFFIX => '.txt'); # create a temporary outfile 
    system ('nohup threader -p $temp_fh $out_fh cdc6.lst &'); #call threader 
    open($outfile, '>>', 'out.txt');
    print $outfile "$out_fh"; # append the content of the temp out to the main outfile
} 

当我尝试运行它时,“我的”会出现很多问题:

“my”变量 $outfile 掩盖了 testfile4.pl 第 22 行同一语句中的早期声明。

“my”变量 $seq_id 掩盖了 testfile4.pl 第 24 行相同范围内的早期声明。

“my”变量 $temp_fh 掩盖了 testfile4.pl 第 26 行同一语句中的早期声明。

“my”变量 $seq_id 掩盖了 testfile4.pl 第 26 行同一语句中的早期声明。

“my”变量 $seq 掩盖了 testfile4.pl 第 26 行同一语句中的早期声明。

testfile4.pl 第 24 行的语法错误,靠近 ") {"

testfile4.pl 第 31 行“}”附近的语法错误

testfile4.pl 的执行由于编译错误而中止。

有人知道这里发生了什么吗?我怎样才能让它运行?

PS 我知道这可能还有很多其他的问题。我是一个初学者,我不需要它整洁或高效,我只需要它工作。

【问题讨论】:

标签: perl bioinformatics


【解决方案1】:

作为提示 - 每当您遇到大量此类错误时,通常是,因为您错过了分号或右括号。

喜欢这一行:

open(my $outfile, '>', 'out.txt') or die
close (my $outfile)

你需要一个分号。您还不需要my 它,因为您已经将$outfile 声明为open 语句的一部分。

当然,打开后立即关闭它也没有多大意义。

(另外 - shebang 行中的 ! 是怎么回事?)

这里还发生了一些其他非常奇怪的事情。喜欢:

print $outfile "$out_fh"; # append the content of the temp out to the main outfile

我不认为那会做你想做的事。因为$out_fh 是一个文件句柄,所以您将打印“GLOB(0xDEADBEEF)”或类似内容。

【讨论】:

  • 谢谢,我想我现在至少已经解决了一些错误。这就是在紧迫的期限内尝试学习 perl 的结果(非常混乱的代码)
  • @Carey 特别是在学习或在紧迫的期限内,使用已知的良好输入和验证输出,一次完成一小部分工作确实很有帮助
  • 最好的技巧是:开启严格和警告。使用Data::Dumper 来检查您在程序中的每一点都获得了您认为自己获得的结果。
猜你喜欢
  • 2021-04-12
  • 1970-01-01
  • 2013-03-26
  • 2023-03-30
  • 1970-01-01
  • 2014-01-10
  • 2012-09-14
  • 2017-12-06
  • 1970-01-01
相关资源
最近更新 更多