Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I am new to perl and I have a python script which I can't understand exactly how it works, but it works :)
My question is, up until now, I call the python script from inside a perl script using `python SCRIPT.py`; and then proceed with doing othe stuff. Is it possible to take the python code and put it inside the perl script somehow, so that I don't have 2 separate scripts?

Replies are listed 'Best First'.
Re: embed python script into perl script?
by AnomalousMonk (Archbishop) on Jan 23, 2012 at 19:22 UTC
    I am new to perl and I have a python script which I can't understand exactly ...

    OK, so we've established that you don't really understand Perl, perhaps don't really understand Python, and certainly don't understand the Python script you're dealing with.

    Is it possible to take the python code and put it inside the perl script ...?

    As others have pointed out, it's possible, and in more than one way. But is it wise? I have to agree with Arunbear's suggestion that your time might be better spent working to gain a better understanding of some combination of Perl, Python and the scripts in question. Your current self-described state of ignorance conjures up notions of very thin ice and very cold, dark waters beneath. Once you have improved your grasp of the issues with which you're grappling, you may find a much simpler and safer path to a solution. You may also find that the whole thing ain't broke and don't need fixing!

      I gotta second that motion ...

      There are lots of great language interpreters out there.   Python is one; Perl is another.   (During the course of any given work-week, I often use both of these and several more, switching between them without pause.)   It is, in fact, very common to find more than one such tool being used in a project at the same time ... and never the twain shall meet.   Simply use each language for its own particular advantages (or, as the case may be, “because it is (already) there,” and ... don’t borrow trouble.   You do not have a problem now.   Do not make one for yourself.   The present situation is quite defensible and serviceable.   It needs neither remedy nor improvement.

Re: embed python script into perl script?
by Arunbear (Prior) on Jan 23, 2012 at 18:17 UTC
    It is possible, though it seems like a false economy. Here is another method:
    #!/usr/bin/perl use autodie; use strict; use warnings; my $code = do { local $/; <DATA> }; my $result = qx/python -c '$code'/; print "$result\n"; __DATA__ def cube(x): return x * x * x print "the cube of 2 is %d" % cube(2)
Re: embed python script into perl script?
by thargas (Deacon) on Jan 23, 2012 at 17:22 UTC
Re: embed python script into perl script?
by Anonymous Monk on Jan 23, 2012 at 17:22 UTC