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

It's certainly doable. If I had more tuits, it would be something I'd consider doing. I'd start with using token_get_all to build a stream of PHP tokens (in PHP!), build up an abstract syntax tree and then compile that down to Perl.

In fact, somebody's already done the hard part.

UPDATE: OK, I've put together a very quick proof of concept. It can successfully translate <?php echo "Hello world"; ?> to print("Hello world"); (here's the relevant test case). Currently the AST only handles a very tiny subset of PHP (just enough to pass that test!) - there's a long list of language constructs that still need to be handled. Once that's been done, implementations are needed for PHP's built-in functions. Contributions welcome.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: Has anyone attempted to create a PHP to Perl converter?
by LanX (Saint) on Nov 13, 2013 at 14:48 UTC
    > a stream of PHP tokens (in PHP!), build up an abstract syntax tree and then compile that down to Perl.

    You can't handle PHP's 'eval($string)' if you parse the syntax in PHP.

    > In fact, somebody's already done the hard part.

    Nope, the hard part of translating dynamic languages (apart eval) is the realization of implicit type coercions in all edge cases.

    This can get so complicated that you might need to treat every single variable / data structure as a tied object.

    After estimating the speed of such programs most similar projects crumble away.

    edit

    You effectively need a nifty JIT compiling magic which notices that weird edge cases aren't touched and resorts to efficient code.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re^2: Has anyone attempted to create a PHP to Perl converter?
by morgon (Priest) on Nov 13, 2013 at 09:58 UTC
    But with that approach you could only deal with pure PHP.

    Do you see a way to deal with PHP-extensions written in C?

    I don't know much about either PHP nor XS but I would assume that without at least some database-module etc (probably written in C) such a port would not have much practical value and making a PHP-extension callable from Perl must be difficult or am I wrong?

      Most PHP extensions either dump a bunch of functions into the global namespace, or define a few classes.

      So to implement a PHP extension, you'd simply dump a few Perl or XS functions into the "PHP::GLOBAL" namespace, or define a few Perl classes

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re^2: Has anyone attempted to create a PHP to Perl converter?
by taint (Chaplain) on Nov 13, 2013 at 14:53 UTC
    Holy crap! Most impressive. Well done tobyink.

    These seem like ripe candidates for CUFP, or would that be Cool uses for PHP?

    Thank you very for taking the time. These are great.

    --Chris

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