将python字符串传递给perl脚本

时间:2018-05-14 11:44:50

标签: shell perl

我想将文件路径从Python传递到Perl脚本。虽然我熟悉Python和Bash,但我对Perl一无所知 我有以下(示例)文件:

return.py

print( 'data/test.txt' )

uniprot.pl

use strict;
use warnings;
use LWP::UserAgent;

my $list = $ARGV[0]; # File containg list of UniProt identifiers.
my $base = 'http://www.uniprot.org';
my $tool = 'uploadlists';

my $contact = ''; # Please set your email address here to help us debug in case of problems.
my $agent = LWP::UserAgent->new(agent => "libwww-perl $contact");
push @{$agent->requests_redirectable}, 'POST';

my $response = $agent->post("$base/$tool/",
                            [ 'file' => [$list],
                              'format' => 'fasta',
                              'from' => 'ACC+ID',
                              'to' => 'ACC',
                            ],
                            'Content_Type' => 'form-data');

while (my $wait = $response->header('Retry-After')) {
  print STDERR "Waiting ($wait)...\n";
  sleep $wait;
  $response = $agent->get($response->base);
}

$response->is_success ?
  print $response->content :
  die 'Failed, got ' . $response->status_line .
    ' for ' . $response->request->uri . "\n";

当我从shell调用perl文件时:perl uniprot.pl data/test.txt它工作正常。

我尝试了不同的方法将python print传递给这个调用,但显然是错误的:

1

python3 return.py | perl uniprot.pl

这将给出:Failed, got 500 Internal Server Error for http://www.uniprot.org/uploadlists/。但是,据我所知,代码有效(如上所述),这必须由错误的管道引起。

2

python3 return.py | perl uniprot.pl -

这将给出:Can't open file -: No such file or directory at /usr/share/perl5/LWP/UserAgent.pm line 476.所以似乎字符串被传递给perl文件,但是perl正在寻找一个完全不同的目录。

3
我更改了这一行:my $list = $ARGV[0]; - 到 - &gt; my $list = <STDIN>;然后再次调用上述命令(因此为1和2)。两者都给出:Can't open file data/test.txt : No such file or directory at /usr/share/perl5/LWP/UserAgent.pm line 476.

问题 如何将字符串从return.py传递到uniprot.pl

1 个答案:

答案 0 :(得分:3)

你需要检查参数是通过命令行参数给出的,还是来自STDIN。

my $file;
if (@ARGV) {
    $file = $ARGV[0];
}
else {
    chomp($file = <STDIN>); # chomp removes linebreak
}