2013-04-21 110 views
1

嗨,我新来stackoverflow。我试图寻找一种方法来打印另一个perl脚本内的另一个perl脚本,我遇到的唯一的建议是使用反斜杠转义变量......但我试过这个,它不起作用。在另一个perl脚本中打印perl脚本?

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

这是我初步的脚本:

#!/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. 
+0

为什么你需要创建新的pl文件?如果需要,最好使用不同的命令行创建一个pl。 – TrueY 2013-04-21 16:00:53

回答

5

使用这里与周围的领先定界符单引号记录。

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>将其读回。