Perthon Python-to-Perl Language Translation
Home
FAQ
To-Do
Design Notes
Change Log
Links
SourceForge Project Page

Perthon -- Python to Perl Language Translation

Download latest release: perthon-0.1-20031201.tar.gz
Download latest development snapshot: perthon.tar.gz

Introduction

The purpose of this project is to automatically translate Python into human-readable Perl using Perl. It is currently making use of Damian Conway's Parse::RecDescent for the parsing/lexing.

Status: This is pre-pre-alpha software (i.e. experimental)

Example run:

Python input:
from math import *
x,n = 5,1
x *= 2+3**3
class
myclass:
   def __init__(s, n):
       s.color = n
   def ok(s,val):
       print val**2
o = myclass("blue")
print o.color
v = [5,6,o]
x,v[n] = 1+2,3
v[1+1]=v[1:n+2][2*n-1].ok(1)
while x > 3 and x < y:
   x = x+1
   y = 3
   while y < 4:
       y = x
       x = 1
       if y > 3: continue
       else: break
def
test(a,b): # func
   a=b
   for x in 1,2,3: y=x; z=x
   return a,b
for x in 1,2,3:
   y=x
   z=x
print 1,2\
 +3*x+cos(
 2+sin(3))
assert 1==1
test(2+3,x/3)
x = {1:2, 3:4+5}
x = 0.2 + 1. + 2E5 + 2.E5 + 2.e+5
y = '234\'234"'
z = "234\"234'"
if x < 10:
   print """multi-line
string"""
elif x > 20:
   print 123
else:
   print 234
Output:
blue
1
1 10.460095472
123
Perl output:
;
($x, $n) = (5, 1);
$x *= 2 + 3**3;
{
   package myclass;
   sub new {
       my($class, $n) = @_;
       my $s = {};
       bless $s, $class;
       $s->{color} = $n;
       return $s;
   }
   sub ok {
       my($s, $val) = @_;
       print $val**2, "\n";
   }
}
$o = new myclass("blue");
print $o->{color}, "\n";
$v = [5, 6, $o];
($x, $v->[$n]) = (1 + 2, 3);
$v->[1 + 1] = [@{$v}[1..$n + 2 - 1]]->[2 * $n - 1]->ok(1);
while($x > 3 and $x < $y) {
   $x = $x + 1;
   $y = 3;
   while($y < 4) {
       $y = $x;
       $x = 1;
       if($y > 3) {
           next;
       }
       else {
           last;
       }
   }
}
sub test {
   my($a, $b) = @_;
   $a = $b;
   foreach my $x (1, 2, 3) {
       $y = $x; $z = $x;
   }
   return ($a, $b);
}
foreach my $x (1, 2, 3) {
   $y = $x;
   $z = $x;
}
print 1, " ", 2 + 3 * $x + cos(2 + sin(3)), "\n";
die if not 1 == 1;
&test(2 + 3, $x / 3);
$x = {1 => 2, 3 => 4 + 5};
$x = 0.2 + 1. + 2E5 + 2.E5 + 2.e+5;
$y = '234\'234"';
$z = "234\"234'";
if($x < 10) {
   print qq(multi-line
   string), "\n";
}
elsif($x > 20) {
   print 123, "\n";
}
else {
   print 234, "\n";
}
Output:
blue
1
1 10.4600954719667
123

Why Perthon?

Although Perthon is still experimental, it could have the following uses:

  • To write Perl programs using a Pythonic syntactic sugar. If you like Perl but (unlike me) don't like it's syntax, you can use Perthon. As always, in Perl, there's more than one way to do it.
  • To convert Python source code to human-readable Perl source code, using Perlisms are much as possible.
  • To run Python code in Perl.
  • To more seamlessly combine Python and Perl code.

License/Credits

Perthon is free software and is licensed under the same terms as Perl itself. Refer to the README file in Perl for details. (c) 2003, David Manura, All Rights Reserved.

Author: David Manura (Contact)

SourceForge.net Logo