2

I have a python function as

import commands
output=commands.getputput("ls -l")
print output
return output

I need to get this return value from python function from my perl script

my($command)=`$python_script_path`;
print $command;

My problem is that by printing the ouput in python i can get my results on screen but i actually require this output in Perl script as well so I would rather return it. How can I achieve it ? Also please note I do not want to use inline python. thanks!!

6
  • For you to print it in your perl script, doesn't it already have to exist in your $command variable? Commented Dec 18, 2012 at 14:34
  • but doesnt the backtick operation do that ? store the command output in that variable ? Commented Dec 18, 2012 at 14:36
  • That is what I am saying, the output of the python script you called will be stored in $command Commented Dec 18, 2012 at 14:37
  • As a side note, the commands module is deprecated in favor of subprocess. Commented Dec 18, 2012 at 14:38
  • but it doent seem to work. I have been wondering why !! i thought i was wrong somewhere Commented Dec 18, 2012 at 14:40

3 Answers 3

2

The commands module is deprecated since 2.6. Use subprocess instead.

p = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE, shell=True)
print p.communicate()[0]
Sign up to request clarification or add additional context in comments.

2 Comments

Wrong way. The OP is calling Python from Perl.
sorry guys but i require commands.getstatusoutput(). Also most of the systems in my college run older python versions.
0

Return codes are numerical values.

Your best bet is to print the output and capture the output in Perl.

This might help: How to call a python script from Perl?

1 Comment

thanks...Isnt that what I have done? am i wrong somewhere in the flow ?
0

You also can open pipe to the Python script right in the file 'open':

open PYTHON, "$python_script_path|";
while(<PYTHON>){
 print;
}
close PYTHON;

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.