2

The Perl file 1.pl:

#!/usr/local/bin/perl -w

($b) = @ARGV;    
$a = 1;
$c = $a + $b;
print "$c\n";

exit;

The python file 1.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
b = 2
res = subprocess.call(["perl", "1.pl", str(b)])
print res

check out put

$python 1.py

output:

3
0

the output should 3,so what's wrong?

3 Answers 3

4

0 is the returncode attribute.

$perl so.pl 2
3
$ echo $?
0            #this gets stored in `res`

Just use :

subprocess.call(["perl", "1.pl", str(b)])

If you don't want that 0 in the output.

You can also use subprocess.check_output to store the output of a command in a variable.

>>> res = subprocess.check_output(["perl", "so.pl", '2'])
>>> print res
3
Sign up to request clarification or add additional context in comments.

1 Comment

subprocess.check_output is great!
2

0 from the python. i. e you are storing the result in res variable and 3 is printing from the perl program

Comments

2

In your perl script you use print to print something to screen, and exit to return a value. Calling exit without a number is equivalent to exit 0, which usually stands for success.

In your case your perl will print 3 and return 0.

Later the python ignores the 3 and prints the 0.

If you want to pass a number from your perl to your python either return that number using exit $c in perl or tell python to check the output of of your perl.

Comments

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.