Calling a Python module from Perl


Question

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?

1
8
8/9/2010 3:50:36 PM

Accepted Answer

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.

19
4/4/2014 8:41:12 AM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon