Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Has anyone attempted to create a PHP to Perl converter?

by sundialsvc4 (Monsignor)
on Nov 13, 2013 at 20:52 UTC ( #1062472=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Has anyone attempted to create a PHP to Perl converter?
in thread Has anyone attempted to create a PHP to Perl converter?

There’s no doubt, of course, that valid PHP can be parsed, for syntax checking and so-on, and that once the source has been reduced to an abstract-syntax tree (AST) form, some of those AST constructs could be expanded into Perl.   The languages are in many ways very similar.   You might be able to do such a thing, say, in an editor-macro for a very smart editor.

If you look beyond the superficial, though, you start getting into some much thornier problems.   For example, in the PHP system, “everything is compiled-in,” and is called by means of (hundreds of ...) functions.   Database access, for example, is implemented in that way.   In the Perl system, per contra, we have DBI.   Now you are going to have to develop a way for the translated Perl code to continue doing things “the PHP way.”   Suddenly, your problem has become quite a bit more complicated, and pretty soon “diminishing returns” are going to be a factor.   And then, there are the ideas that simply do not exist at all in Perl, such as the intermingling of HTML and PHP in a file, which was a founding concept of PHP.


Comment on Re^3: Has anyone attempted to create a PHP to Perl converter?
Re^4: Has anyone attempted to create a PHP to Perl converter?
by tobyink (Abbot) on Nov 13, 2013 at 22:39 UTC

    "And then, there are the ideas that simply do not exist at all in Perl, such as the intermingling of HTML and PHP in a file, which was a founding concept of PHP."

    This is something that's actually incredibly easy to implement. This PHP:

    ?>Hello<?php

    Becomes this in Perl:

    print "Hello";

    There are far harder things to implement - PHP's idea of arrays differs greatly from Perl's; PHP has different ideas about passing arguments to functions by value or reference; different variable scoping; different behaviour of the ==, <, >, etc operators; and so on.

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

      What I mean, Toby, is just that “within the Perl system, most-specifically versus the PHP system,” there is n-o concept of the language-interpreter being “embedded within” a surrounding file “that is to be served.”   Although this concept is a foundation of PHP, I can’t readily recall any other language which does the same thing.

      Still, you quite-correctly heap coals upon the same fire:   there are, in fact, a great many differences between the two languages which would make a transliteration between them ... let alone an automatic one ... quite difficult to achieve satisfactorily.   And this may well be the best answer to the OP’s question:   “Even if it could be done, it’s just not worth doing.”   We don’t have to do it.   There is no advantage to be had in picking that particular fight.   We have all of these language systems at our beck-and-call, side by side.   We can, when we need to, make them work together.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1062472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2014-04-24 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls