in reply to Has anyone attempted to create a PHP to Perl converter?

I recently took on a small project where I had to rewrite a PHP program in Perl. While I don't want to say an automatic converter is impossible, my own experience was that human involvement was beneficial to the project.

Compiling C down to machine code works because nobody ever has to look at the machine code again. But in translating from one high level language to another, this is a source-code transition. I assume that the new source code would need to be maintained over time. Just looking at the mess made by automatic HTML/CSS generators (which is less complex than a high level language like Perl) should be warning enough to realize that an automated translation would produce messy, read-only code.

But who knows... maybe the next reply in this thread will have a perfect solution. ;)


Dave

  • Comment on Re: Has anyone attempted to create a PHP to Perl converter?

Replies are listed 'Best First'.
Re^2: Has anyone attempted to create a PHP to Perl converter?
by taint (Chaplain) on Nov 13, 2013 at 07:11 UTC
    Thanks for your reply.

    I think given my current (Perl) skillset. It would indeed require the "human interaction" you describe. Much of it seems to be alot of (s)print(f) sort of stuff. And much of the syntax even closely mimics Perl. So it really doesn't seem to me, a, "quantum leap", or anything. But, as I stated above; it'll probably take some accomplished Perl RE skills.

    Thanks again, for taking the time to respond.

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;

      It's a little more than a regexp problem. You can't exactly substitute a PHP array with a Perl array, or even a Perl hash, because the PHP array is actually an ordered map. An ordered map has different performance and semantic characteristics from a Perl array or a Perl hash. And that's just where the differences begin.

      A human can look and say, "That algorithm that uses a PHP array can be expressed with a Perl array." Or "This other algorithm needs a Perl hash." Or "This algorithm is going to require a hash, and an array crossreference to maintain order." A dumb tool would be forced to make compromises in every case; it would have to maintain the hash, and an ordered list to emulate an ordered map. This is not to say that an ordered map is more powerful, but that it's a different container. In specific cases, Perl could mimic enough of its behavior using an array, and in others, enough of its behavior using a hash. But in a general case, both would have to be used together. Programming for the general case, then, would be inefficient for many specific uses. A human can make the distinction, or even rewrite the algorithm to play to Perl's strengths instead of PHP's strengths.

      Once that problem is solved, there will be many more paradigm mismatches, each one that could be gracefully handled by a human, but where a machine would need to understand the underlying purpose of code segments, which is probably beyond the capabilities of any regexp engine.


      Dave

        Well stated davido .

        Seems I grossly understated/underestimated.

        --Chris

        #!/usr/bin/perl -Tw
        use Perl::Always or die;
        my $perl_version = (5.12.5);
        print $perl_version;
Re^2: Has anyone attempted to create a PHP to Perl converter?
by einhverfr (Friar) on Nov 19, 2013 at 06:40 UTC

    This makes me think that the PHP CPAN module is a good thing in this regard, and that frameworks for transition may be better than code converters.

    In general rewriting apps in another language means rethinking everything because there are all sorts of things that just don't quite match between apps. Something to ease the transition might be very good. So for example, despite my skepticism I think the following could be helpful

    • Boilerplate converters. Take a PHP object definition and turn it into a basic Moo or Moose definition. Don't port the methods, just the boilerplate.
    • Bridging type classes. The ability to have a PHP Array inside your Perl code may ease the transition quite a bit.
    • Web application frameworks written with conversion in mind.

    These strike me as both easier and less work than a full-blown code converter but also provide all the basic infrastructure to allow a human to conversion relatively quickly and easily...