首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >管道python字符串到perl脚本

管道python字符串到perl脚本
EN

Stack Overflow用户
提问于 2018-05-14 11:44:50
回答 1查看 260关注 0票数 1

我想将文件路径从Python输送到Perl脚本。虽然我熟悉Python和Bash,但我对Perl一无所知。

我有以下(示例)文件:

return.py

代码语言:javascript
复制
print( 'data/test.txt' )

uniprot.pl

代码语言:javascript
复制
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打印到这个调用,但显然是错误的:

1.

代码语言:javascript
复制
python3 return.py | perl uniprot.pl

这将给:Failed, got 500 Internal Server Error for http://www.uniprot.org/uploadlists/。但是,正如我所知,代码工作(如上所述),这必须是由错误的管道造成的。

2

代码语言:javascript
复制
python3 return.py | perl uniprot.pl -

这将给出: perl,所以看起来字符串被传递给perl文件,但是perl正在寻找一个完全不同的目录。

3

我更改了这一行:my $list = $ARGV[0]; --to-> 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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-14 12:02:36

您需要检查参数是通过命令行参数还是通过STDIN提供的。

代码语言:javascript
复制
my $file;
if (@ARGV) {
    $file = $ARGV[0];
}
else {
    chomp($file = <STDIN>); # chomp removes linebreak
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50329497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档