20

I've got a Perl script that I want to invoke from a Python script. I've been looking all over, and haven't been successful. I'm basically trying to call the Perl script sending 1 variable to it, but don't need the output of the Perl script, as it is a self contained program.

What I've come up with so far is:

var = "/some/file/path/"
pipe = subprocess.Popen(["./uireplace.pl", var], stdin=subprocess.PIPE)
pipe.stdin.write(var)
pipe.stdin.close()

Only just started Python programming, so I'm sure the above is total nonsense. Any help would be much appreciated.

5 Answers 5

17

Just do:

var = "/some/file/path/"
pipe = subprocess.Popen(["perl", "uireplace.pl", var])
Sign up to request clarification or add additional context in comments.

5 Comments

Just a note: since the OP says he doesn't want to get the output, Popen is not required. Using subprocess.call or subprocess.check_call is better.
@sukhbir: i think when the OP say that he don't need the output, i can also "assume" that he also don't want to wait until the perl script finish which what subprocess.call or subprocess.check_call does :)
so using this if the perl script were to run in terminal and i want to rerun it everytime the perl script crashed, and would have to use an if pipe = error message than rerun the perl code or something like that?
@pyCthon: Sorry but i didn't understand very well your question, what i understood is that you're running a Perl script using the code above and you want to make sure that if the Perl script crash, it should be re-spawn, right ?
@pyCthon; Well i am not sure about the context where you want to do this, but one way that i can think about is to execute Popen.poll (docs.python.org/2/library/subprocess.html#subprocess.Popen.poll) at a fix interval to check if the process is alive or not, so you can re-spawn it when it's crash i.e. Popen.pool() != 0, you can run this mechanism in a Thread for example, other ways will involve using something like upstart.ubuntu.com or supervisord.org .
16

If you just want to open a pipe to a perl interpreter, you're on the right track. The only thing I think you're missing is that the perl script itself is not an executable. So you need to do this:

var = "/some/file/path/"
pipe = subprocess.Popen(["perl", "./uireplace.pl", var], stdin=subprocess.PIPE)
pipe.stdin.write(var)
pipe.stdin.close()

2 Comments

the ./ part in the script call is useless and i can't see the use of the two last line, and this two last line can lead to bugs !!
If you are providing absolute path and perl executable then ./ is not in use.
3

You could try the subprocess.call() method. It won't return output from the command you're invoking, but rather the return code to indicate if the execution was successful.

var = "/some/file/path"
retcode = subprocess.call(["./uireplace.pl", var])
if retcode == 0:
    print("Passed!")
else:
    print("Failed!")

Make sure you're Perl script is executable. Otherwise, you can include the Perl interpreter in your command (something like this):

subprocess.call(["/usr/bin/perl", "./uireplace.pl", var])

Comments

2

Would you like to pass var as a parameter, on stdin or both? To pass it as a parameter, use

subprocess.call(["./uireplace.pl", var])

To pipe it to stdin, use

pipe = subprocess.Popen("./uireplace.pl", stdin=subprocess.PIPE)
pipe.communicate(var)

Both code snippets require uireplace.pl to be executable. If it is not, you can use

pipe = subprocess.Popen(["perl", "./uireplace.pl"], stdin=subprocess.PIPE)
pipe.communicate(var)

Comments

0

I Hope this can help you. Do not know how to do that otherwise.

1 Comment

thank you for the reference... it was created and supported by activestate.. they seems dropped 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.