Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Python module from Perl

I created a module in Python which provides about a dozen functionalities. While it will be mostly used from within Python, there is a good fraction of legacy users which will be calling it from Perl.

What is the best way to make a plug in to this module? My thoughts are:

  1. Provide the functionalities as command line utilities and make system calls
  2. Create some sort of server and handle RPC calls (say, via JSON RPC)

Any advise?

like image 609
Escualo Avatar asked Aug 09 '10 15:08

Escualo


2 Answers

One other choice is to inline Python directly in your Perl script, using Inline::Python.

This may be simpler than other solutions, and only requires one additional module.

like image 197
mfontani Avatar answered Oct 01 '22 16:10

mfontani


In the short run the easiest solution is to use Inline::Python. Closely followed by calling a command-line script.

In the long run, using a server to provide RPC functionality or simply calling a command-line script will give you the most future proof solution.

Why?

Becuase that way you aren't tied to Perl or Python as the language used to build the systems that consume the services provided by your library. Either method creates a clear, language independent interface that you can use with whatever development environment you adopt.

Depending on your needs any of the presented options may be the "best choice". Depending on how your needs evolve over time, a different choice may be revealed as "best".

My approach to this would be to ask a couple of questions:

How often do you change development tools. You've switched to Python from Perl. Did you start with Tcl and go to Perl? Are you going to switch to the exciting new language X in 1, 5 or 10 years? If you change tools 'often' (whatever that means) emphasize cross tool compatibility.

How fast is fast enough? Is the start up time for command line solutions ok? Does Inline::Python slow things down too much (you are still initializing a Python interpreter, it's just embedded in your Perl interpreter)?

Based on the answers to these questions, I would do the simplest thing that is likely to work.

My guess is that means in order:

  1. Inline::Python
  2. Command line scripts
  3. Build an RPC server
like image 22
daotoad Avatar answered Oct 01 '22 16:10

daotoad