Skip to content

Instantly share code, notes, and snippets.

@pdl

pdl/pl2py.pl

Created Oct 23, 2012
Embed
What would you like to do?
Convert perl scripts to python with the awesome power of regular expressions.
#!perl -w
=head1 NAME
pl2py.pl
=head1 DESCRIPTION
Attempts to convert perl scripts to python with the awesome power of regular expressions.
=head1 BUGS
This will (probably) not actually produce runnable python files. But it saves a lot of work converting some perl to python.
More useful if your perl code is well-indented to start with (consider using a source formatter first).
=head1 AUTHOR
Daniel Perrett C<< dperrett@cambridge.org >>
=cut
my $state;
while (my $sLine = <>)
{
$sLine =~ tr/$//d;
$sLine =~ s/(?!<\d)\.(?!\d)/+/g;
$sLine =~ s/::/./g;
if ($state->{'pod'})
{
$sLine =~ s/^=(?!cut)//g;
}
elsif ($sLine =~ s/^=(?!cut)/"""/)
{
$state->{'pod'} = 1;
}
if ($sLine =~ s/^=cut/"""/)
{
$state->{'pod'} = 0;
}
$sLine =~ s/^\s*package (.*?);/class $1:/g;
$sLine =~ s/^\s*use /import /g;
$sLine =~ s/^\bundef\b/None/g;
$sLine =~ s/^\beq\b/==/g;
$sLine =~ s/^\bge\b/>=/g;
$sLine =~ s/^\ble\b/=</g;
$sLine =~ s/^\bne\b/!=/g;
$sLine =~ s/^\bgt\b/>/g;
$sLine =~ s/^\blt\b/</g;
$sLine =~ s/^\|\|/or/g;
$sLine =~ s/^&&/and/g;
$sLine =~ s/\s+{(['"])(.*)\1}/.$2/g;
#$sLine =~ s/^\s*sub\s*([\w]+)\s*(?:\(([^)]+)\))\s*\{?/def $1 ($2):/g;
$sLine =~ s/\bsub ([\w]+)(?:\(([^)]+)\))?\s*\{?/def $1:/g;
$sLine =~ s/\bmy ([\w]+)\s*=/$1 =/g;
$sLine =~ s/\bmy ([\w]+);//g;
$sLine =~ s/!/ not /g;
$sLine =~ s/->\{/./g;
$sLine =~ s/->\[/./g;
$sLine =~ s/->/./g;
$sLine =~ s/\{$/:/g;
$sLine =~ s/\}//g;
$sLine =~ s/;$//g;
print STDOUT $sLine;
}
@arne-cl

This comment has been minimized.

Copy link

@arne-cl arne-cl commented Jul 16, 2014

Hi Daniel,

this script doesn't seem to handle @array variables, e.g.

for (my $n=0; $n<scalar @$Y; $n++) {
    for (my $m=0; $m<scalar @{$Y->[$n]}; $m++) {
        $allLabels{$Y->[$n][$m]} = 1;
    }
}

Best regards,
Arne

@nmz787

This comment has been minimized.

Copy link

@nmz787 nmz787 commented Aug 27, 2014

This isn't in Python! :)

@Hasimir

This comment has been minimized.

Copy link

@Hasimir Hasimir commented Jan 1, 2016

Brilliant! :)

It doesn't produce working code that I've seen, but it does something far more important; it makes it possible to decipher just wtf some random perl coder from aeons gone by was trying to do, so you can reproduce the end result. Which is what counts.

@andrew-morris

This comment has been minimized.

Copy link

@andrew-morris andrew-morris commented Feb 18, 2016

I read this and my nose started to bleed

@warewolf

This comment has been minimized.

Copy link

@warewolf warewolf commented Feb 18, 2016

why, why would you do this?

@colin-morrell

This comment has been minimized.

Copy link

@colin-morrell colin-morrell commented Feb 18, 2016

I think I just contracted autism

@colinhunt

This comment has been minimized.

Copy link

@colinhunt colinhunt commented Jun 1, 2017

What if I tried to convert this script to python... woah.

@geraldkrug

This comment has been minimized.

Copy link

@geraldkrug geraldkrug commented Sep 26, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment