python to perl (convert or translate or parse)

hi, does anyone know (or have any information on) how to convert/translate and/or parse a python code into perl? or is it too ambiguous to do so? i need to at least parse a phyton code into perl....does anyone know where i might find (or how i might do it) easyly? i mean..i could do it with tons of regular expressions n stuff.....but is there any other way?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In the time it would take to do a good job of this, you could have a working Parrot. Parrot is the project to write a common backend for scripting languages like Perl, Python, and Ruby and is already part of the plans for Perl6.

Meanwhile, maybe you could say a bit more about what your immediate need is so we can maybe offer sensible approaches. It is impractical to mechanically translate a whole Python application into Perl. But if you just have some small pieces of Python that are built according to some template, we may be able to suggest a way to re-interpret those snippets into working Perl.
TraltixxAuthor Commented:
well...i have to translate or at least parse whoe of python to perl...i mean id input a python sourcecode and get a perl translation..saying which are variables, which are if statement (and where to go if a certain condition), where to loop and where are subroutines (and where they are called and returned)...thus the template for input would probably be a python sourcedcode that has these elements in them..and perl will try to at least break it up and translate it. as i am unfamiliar with python, how can this be acomplished?

btw, when is perl 6 and parrot going to be released?
Oh, the release of Perl6 and Parrot is not coming very soon, but to do a general job of translating would take a long time, too.

Let's back up a few steps. Why do you believe you need to translate Python into Perl? Python will run in all the same places that Perl does. If you have a standard Python library that you're dealing with, there may well be a closely equivalent Perl library available to do the same thing. You can run a Python script from perl and vice versa. So please explain a bit more and maybe we can come up with an answer for you.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

TraltixxAuthor Commented:
well...the thing is i need to read a python/perl sourcecode process it the perl way...thus whatever the sourcecode was, the perl translator will understand and process it...that is essentially what i want to do
Since I cannot figure out what you are trying to do, and you won't tell me, all I can do is wish you the best of luck.
TraltixxAuthor Commented:
sigh..ok..i was just trying to build a perl script that would take a python code n output its perl equivalent (parts that arent  translateable or arent implemented in perl will be ignored and commented). im doing this as a starting point for a universal translator....anyway..any other helpers?
I have experience using the Parse::RecDescent module ( , to compile a language into bytecodes.  You might alternatively try Parse::Yapp, or one of the other parsing/lexing modules available on CPAN.  However, be warned that implementing such a compiler/translator is not an easy task.  It took me a couple of weeks to implement a language that was fairly basic yet robust.  Getting the compiler to work on some correct types of input is easy (a few days); getting the compiler to recognize all good input and cleanly fail on all bad input is much harder.

As an interesting sidenote, here's a (partially implemented) Perl-to-Python translator:

It uses a modified B::Deparse module.  When translating as such between high-level languages for the purpose of the generated code to be read by humans, there is a need for generated Python code to not just exectute but to look Pythonic rather than as Python code written in Perlisms.  Otherwise, you minus well just embed a real Python interpreter into Perl.  I would think that implementing a Perl-to-Python translator would be much harder than doing the reverse since Perl has a much richer syntax.  So, I have some doubts on whether Perl-to-Python translation is even a useful/possible pursuit.  A Python-to-Perl translator would have the advantage than Pythonic code is easier to express in Perl than vice-versa.  Parrot seems to be the way to go.

I don't think you've told us though essentially why you feel the need to perform automated Python-to-Perl translation.  It's big task, and there must be some justification for expending the effort.  It does sound like an interesting project, which I may be willing to help on developing a CPAN module if others are interested.  :)

Also of interest are various CPAN modules for easing Perl-Python integration:
Most take the (sensible) approach of embedding a Python interpreter into Perl rather than trying to reimplement Python in Perl (similar to Jython, which reimplements Python on the Java VM).  However, none of these are intended as source-code translators.
TraltixxAuthor Commented:
thanks for the comments....firstly, the project is part of a user-friendly debugger....the only part left if the input section which takes python (or other languages) or perl and debugs it in perl.
anyway,ive looked into YAPP, and it doesnt seem to parse python....(or i didnt find a way how to)
and yes, i do think translating perl to python is a much harder task, thats why the main script/program will be written in perl instead of python.......
anyway, whats wrong with source-code translators? arent there any? i mean, thats the whole point....
oh well..thanks for your help....does anyone have any other ideas?
TraltixxAuthor Commented:
also on a side note the Parse::RecDescent  looks promising, but is there a way to convert it to perl no matter what bytecods it is from. anyway, just wondering...thanks again for your help
There are source code translators.  I haven't used any, but ANTLR (, which has lots of good info on its web site, has been used to build some such translators (e.g. VB --> C#:

To build a Python recognizer (using Parse::RecDescent, Parse::Yapp, ANTLR, or whatever), the place to start is with the Python grammar:

Convert this grammar into the format needed by the particular tool so that the tool can build a parse tree.  Then add the necessary code to convert that parse tree into Perl source code.

In fact, you you may have inspired in this direction.  I'm converting grammar.txt into the format needed by Parse::RecDescent and have begun adding some action handlers to translate Python assignment_stmt's to corresponding Perl code.  For example, here's one crude rule so far:

# original Python rule:
# or_test ::=
#              and_test | or_test "or" and_test
or_test : and_test(s /or/)
    my $ands = $item{'and_test(s)'};
    my $perl = join(' or ', map "$$_{perl}", @$ands);
    {perl => $perl}

Actually, this rule would translate the Python "1 or 0 or 1" into the equivalent Perl "1 or 0 or 1" (not very interesting!).

If I maintain interest, I'll post a link to the code here.  My impetus for doing this is so that the next time a Python user says "The Python syntax is so much cleaner than the Perl line-noise, and everyone should use Python," we Perl monks can wrap the Python-->Perl translator in a Filter::Simple and respond,

  use PythonFilter;

  def hello(who):
      print "Hello", who, ", there's more than one way to do it!"


> also on a side note the Parse::RecDescent  looks promising,
> but is there a way to convert it to perl no matter what bytecods
> it is from

The two "its" seem ambiguous in reference, but Parse::RecDescent is a Perl module written entirely in Perl.  You feed Parse::RecDescent a grammar file or string (having syntax similar to the grammar.txt above) that also contains actions (Perl code blocks that get executed when grammar rules are matched).  These actions must be written to generate corrsponding Perl code at various stages in the parse of the Python code.  Parse::RecDescent uses this specification to generate the Perl code for a corresponding compiler.  You can import the generated compiler into your own program since the compiler is simply a Perl module.  You then would feed in Python code into that compiler, which would spit out the corresponding Perl code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Here's my very initial implementation:


x = 5
y = 2+3**3
x,y = 1+2,3
while x > 3: x = x+1

$x = 5;

$y = 2 + 3**3;
($x, $y) = (1 + 2, 3);
while($x > 3) {
$x = $x + 1;
TraltixxAuthor Commented:
wow......well, at least its a start..i dont get one bit tho..u said
"If I maintain interest, I'll post a link to the code here.  My impetus for doing this is so that the next time a Python user says "The Python syntax is so much cleaner than the Perl line-noise, and everyone should use Python," we Perl monks can wrap the Python-->Perl translator in a Filter::Simple and respond,"
meaning that it would be easier to code/understand in perl rather in python? is interesing (tho some parts of creating a module i still dont get..but ill leave that for another thread)..
but the python grammer
u gave the link that all the python grammar or only some of it? because i would expect python to have much more....
anyway..whats next? so i can get into it as well....and thanks for sharing the initial implementation
Perl allows the use of custom preprocessors in order to extend the language (e.g. by using Filter::Simple as described on  My idea is that we wrap the Python --> Perl translator in such a filter so that it is really convenient to use.  You could even mix Perl and Python code within the same source file (if you really wish, even though it might not be in good design taste--Perl in Python are probably best kept in separate files).  It's kind-of like the .NET/JVM/Parrot concept on the existing Perl 5.x runtime.

sub hello {
    return "hello";


use Python;

def world():
    print hello(), "world"

But the main benefit is seemless intergration between Perl and Python-like code.  A Python-like program could directly manipulate a Perl hash or invoke code from Perl CPAN modules.  Maybe we can even entice back those who have strayed away from Perl into the land of Python.

The Python grammar is complete (yes, it is fairly simple, which is good for Perthon).  It's linked from the authoritative Python Language Reference (, which would include anything else that's needed (e.g. tokenization rules).

If I continue to maintain interest, I'll post this on sourceforge or something.  For any interim updates, I'll note them here.
I'll cheerfully admit I was wrong. I can't say how impressed I am with what you've done, Ext2!
Made some good progress tonight.  The pre-pre-alpha Perthon compiler is now partly usable and packaged for download.  As seen from the grammar file, some major constructs are still missing, like, um, if statements.  However, if you really need an if statement, you can tentatively rewrite it as a while ;)  If you have any questions, read the source code!
TraltixxAuthor Commented:
well...ok..firstly im on vacation (for 3 months) so id have to pause this for a sec.....but ill give u the point anyway and ill resume (or start) on it as soon as i get back (btw..i dont have my perl on my pc i cant rest anything)
thanks very much for your help
Just to let everyone know, Perthon is now on SourceForge:

And thanks, Traltixx, for bringing up the idea!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.