0

I would like to call a Perl script with arguments from my Python program. so far I have been able to do it using subprocess and piping

#!/usr/bin/python
import subprocess
var = "world"
pipe = subprocess.Popen(["./x.pl", var], stdout=subprocess.PIPE)
result = pipe.stdout.read()
print result

#!/usr/bin/perl
use strict;
use warnings;
my $name = shift;
print "Hello $name!\n";

But as the name states, it's just piping the output.

What I need is the Perl script returning some value (for example here it would return the string instead of printing) and Python catching that value and using it.

I need to do this because the Perl script already exists and I'm trying to convert it from CGI (returns a value for each call) to Python (a Websocket server acts as proxy between the client and script)

1
  • If you wish to change the way the scripts operate and interact (e.g. do not start the script each time), there are countless ways, it's really up to you. They include specially crafted alternatives to CGI like FastCGI, SCGI and WSGI. Commented Jun 7, 2016 at 20:16

1 Answer 1

3

Personally, I wouldn't change the Perl script at all. It's already designed to handle a request from its parent process and return its response via stdout. Just provide an appropriate CGI environment for it.

If you want to return transmit a more complex structure, you'll need to serialize it. JSON is usually the best choice.

#!/usr/bin/perl
use strict;
use warnings;
use JSON::XS qw( encode_json );
my $name = shift;
print encode_json({ code => 200, content => "Hello, World!\n" });
Sign up to request clarification or add additional context in comments.

6 Comments

is that it? I thought about that but I tried not to change/adapt the original script for my special case
Sorry, my internet cut out as I was about to add to my answer. It's been added now.
Thanks! The first part of your reply is what I think I was looking for.. now do you know of any resources I could check out to learn how to "provide an appropriate CGI environment" to a perl script called inside a python program? another reason I don't want to modify the original script is that I don't want to have two separate perl scripts (which are doing the same) for two different technologies (AJAX and WebSocket). The Perl script is the heart of the webapp and I foresee a lot of changes to it.
It's really just a bunch of env vars + the request body on stdin (for requests with bodys, so not HEAD and GET). The response is a MIME document on stdout. I found an attempt to formally define it here, but it might be easier just to hack your way using the this information.
I would consider myself a beginner in the area and I have no idea what you're describing or how to do it.. how can I call the CGI Perl script from Python? is there a function? I have to POST the data from Python to the script (both in backend) and catch/use the results
|

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.