1

嗨,我是 stackoverflow 的新手。我已经尝试寻找一种在不同的 perl 脚本中打印另一个 perl 脚本的方法,我遇到的唯一建议是使用反斜杠转义变量……但我尝试了这个,但它不起作用。

我的目标是编写一个 perl 脚本来制作一堆新的 perl 脚本,但因为它不允许我在打印“”中使用变量/数组/等。有没有解决的办法?提前致谢!

这是我的初步脚本:

    #!/usr/bin/perl
    use warnings;
    use strict;

    my $idfile = $ARGV[0];
    open (IDFILE,'<',$idfile)
    or die "Could not open $idfile \n";

    my $outfile_name;
    my $outfile = $outfile_name."pl";
    open (OUTFILE, '>', $outfile)
    or die "Could not open $outfile \n";

    while (my $line = <IDFILE>) {
        chomp ($line);
        if ($line =~ /(T4-GC_[0-9]+)/) {
            my $outfile_name = "Pull_".$line;
            my $script = "
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Bio::SearchIO;
    use Bio::SeqIO;

    my @ARGV = glob("*.fa");

    foreach my $fil (@ARGV) {
        my $seqio  = Bio::SeqIO->new(-format => 'fasta', -file  => $fil);
            while (my $seqobj = $seqio->next_seq) {
            my $seqid = $seqobj->display_id;
            $fil =~ /([A-Z]+[0-9]+)/;
            my $phage_name = $1;
            my $id = $seqid."|".$phage_name;
            my $nuc = $seqobj->seq();
            if ($seqid =~ /**$line**/) {
                    print ">$id\n$nuc\n";
            }
    }
    }"
        print OUTFILE $script;
        }
    }

这是我回来的错误:

    String found where operator expected at make_perl_pull_genes_files.pl line 33, near                 "my $id = $seqid.""
      (Might be a runaway multi-line "" string starting on line 25)
        (Missing semicolon on previous line?)
    Backslash found where operator expected at make_perl_pull_genes_files.pl line 36, near "$id\"
      (Might be a runaway multi-line "" string starting on line 33)
        (Missing operator before \?)
    Backslash found where operator expected at make_perl_pull_genes_files.pl line 36, near "$nuc\"
        (Missing operator before \?)
    String found where operator expected at make_perl_pull_genes_files.pl line 39, near "}""
      (Might be a runaway multi-line "" string starting on line 36)
        (Missing semicolon on previous line?)
    syntax error at make_perl_pull_genes_files.pl line 25, near "*."
      (Might be a runaway multi-line "" string starting on line 18)
    Global symbol "$id" requires explicit package name at make_perl_pull_genes_files.pl line 36.
    Global symbol "$nuc" requires explicit package name at make_perl_pull_genes_files.pl line 36.
    Execution of make_perl_pull_genes_files.pl aborted due to compilation errors.
4

1 回答 1

5

使用带单引号的 HERE 文档围绕前导分隔符。

print <<'EOT';
#!/usr/bin/perl
use warnings;
use strict;
use Bio::SearchIO;
use Bio::SeqIO;

my @ARGV = glob("*.fa");

foreach my $fil (@ARGV) {
  my $seqio  = Bio::SeqIO->new(-format => 'fasta', -file  => $fil);
  while (my $seqobj = $seqio->next_seq) {
    my $seqid = $seqobj->display_id;
    $fil =~ /([A-Z]+[0-9]+)/;
    my $phage_name = $1;
    my $id = $seqid."|".$phage_name;
    my $nuc = $seqobj->seq();
    if ($seqid =~ /**$line**/) {
      print ">$id\n$nuc\n";
    }
  }
}
EOT

请注意,尾随分隔符必须由换行符拥抱:\nTRAILING\n 在您的源代码中。例如,不要尝试缩进它。您可以在源文件中填充这样的文本的另一个位置超出了__DATA__一行。然后,您将通过<DATA>.

于 2013-04-21T04:12:29.433 回答