Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Perl script from Python, piping input to it?

I'm hacking some support for DomainKeys and DKIM into an open source email marketing program, which uses a python script to send the actual emails via SMTP. I decided to go the quick and dirty route, and just write a perl script that accepts an email message from STDIN, signs it, then returns it signed.

What I would like to do, is from the python script, pipe the email text that's in a string to the perl script, and store the result in another variable, so I can send the email signed. I'm not exactly a python guru, however, and I can't seem to find a good way to do this. I'm pretty sure I can use something like os.system for this, but piping a variable to the perl script is something that seems to elude me.

In short: How can I pipe a variable from a python script, to a perl script, and store the result in Python?

EDIT: I forgot to include that the system I'm working with only has python v2.3

like image 266
Alex Fort Avatar asked Apr 28 '09 14:04

Alex Fort


People also ask

How do I call a Perl script from Python?

Type "pyth. RunPerl. ext;" where "Full Path To File" is the full path filename of your Perl file. This will cause Python to execute the Perl file, then continue down the line with the rest of your Python code.

Can we convert Perl script to Python?

Here are the simple steps to convert PERL scripts to Python. Remove all ';' at the end of the line. Remove all curly brackets and adjust indentation. Convert variables names from $x, %x or @x to x.


1 Answers

Use subprocess. Here is the Python script:

#!/usr/bin/python

import subprocess

var = "world"

pipe = subprocess.Popen(["./x.pl", var], stdout=subprocess.PIPE)

result = pipe.stdout.read()

print result

And here is the Perl script:

#!/usr/bin/perl

use strict;
use warnings;

my $name = shift;

print "Hello $name!\n";
like image 156
Chas. Owens Avatar answered Oct 05 '22 03:10

Chas. Owens