user574490
user574490

Reputation: 203

Call Perl script from Python

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.

Upvotes: 20

Views: 63515

Answers (5)

Ken Kinder
Ken Kinder

Reputation: 13170

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()

Upvotes: 16

mouad
mouad

Reputation: 70079

Just do:

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

Upvotes: 17

bedwyr
bedwyr

Reputation: 5904

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])

Upvotes: 3

Sven Marnach
Sven Marnach

Reputation: 602635

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)

Upvotes: 2

Donovan
Donovan

Reputation: 6132

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

Upvotes: 0

Related Questions