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.

twe4ked
  • 2,832
  • 21
  • 24
user574490
  • 203
  • 1
  • 2
  • 4

5 Answers5

17

Just do:

var = "/some/file/path/"
pipe = subprocess.Popen(["perl", "uireplace.pl", var])
mouad
  • 67,571
  • 18
  • 114
  • 106
  • 1
    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. – user225312 Jan 13 '11 at 16:11
  • 3
    @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 :) – mouad Jan 13 '11 at 16:14
  • 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 Dec 05 '12 at 00:26
  • @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 ? – mouad Dec 06 '12 at 20:16
  • @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`` (http://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 http://upstart.ubuntu.com/ or http://supervisord.org/ . – mouad Dec 07 '12 at 11:41
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()
Ken Kinder
  • 12,654
  • 6
  • 50
  • 70
  • 1
    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 !! – mouad Jan 13 '11 at 16:21
  • 1
    If you are providing absolute path and perl executable then ./ is not in use. – bimlesh sharma Oct 10 '13 at 18:16
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])
bedwyr
  • 5,774
  • 4
  • 31
  • 49
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)
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
0

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

Donovan
  • 6,002
  • 5
  • 41
  • 55
  • thank you for the reference... it was created and supported by activestate.. they seems dropped it? – Tagar Mar 08 '16 at 01:18