1

I want to pipe a file path from Python to a Perl script. Although I'm familiar with Python and Bash I don't known anything about Perl.
I have the following (example) files:

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";

When I call the perl file from the shell like: perl uniprot.pl data/test.txt it works fine.

I tried different approaches to pipe the python print to this call, but apparently the wrong ones:

1.

python3 return.py | perl uniprot.pl

This will give: Failed, got 500 Internal Server Error for http://www.uniprot.org/uploadlists/. However as I know the code works (as said above) this has to be caused by wrong piping.

2

python3 return.py | perl uniprot.pl -

This will give: Can't open file -: No such file or directory at /usr/share/perl5/LWP/UserAgent.pm line 476. So it seems that the string is passed to the perl file, however perl is looking in a complete different directory.

3
I changed this line: my $list = $ARGV[0]; --to--> my $list = <STDIN>; and then again calling both the above commands (thus 1 and 2). Both give: Can't open file data/test.txt : No such file or directory at /usr/share/perl5/LWP/UserAgent.pm line 476.


Question How can I pass the string from return.py to uniprot.pl?

3
  • Definitly not a Python question - you'd have the same issue with a C program, a shell script or another perl script. Commented May 14, 2018 at 12:17
  • Well yes looking back, however I wasn't sure whether I actually should have printed the string or should have done otherwise @brunodesthuilliers Commented May 14, 2018 at 12:28
  • If this is the excepted output from your program then yes printing it the right thing to do. Commented May 14, 2018 at 13:35

1 Answer 1

3

You need to check wether the argument is given via command line arguments or comes via STDIN.

my $file;
if (@ARGV) {
    $file = $ARGV[0];
}
else {
    chomp($file = <STDIN>); # chomp removes linebreak
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou so much!
That only gives you the first line of input though, doesn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.