开发者

Advice on coding Perl to work like PHP

开发者 https://www.devze.com 2023-01-02 18:04 出处:网络
I am first and foremost a perl coder, but like many, also code in PHP for client work, especially web apps.

I am first and foremost a perl coder, but like many, also code in PHP for client work, especially web apps.

I am finding that I am duplicating a lot of my projects in the two languages, but using different paradigms (e.g. for handling cgi input and session data) or functions.

What I would like to do is start to code my Perl in a way which is structured more like PHP, so that I a) am keeping one paradigm in my head b) can more quickly port over scripts from one to the other

Specifically, I am asking if people could advise how you might do the followi开发者_运维知识库ng in perl?

1) Reproduce the functionality of $_SESSION, $_GET etc. e.g. by wrapping up the param() method of CGI.pm, a session library?

2) Templating library that is similar to PHP I am used to mixing my code and HTML in the PHP convention. e.g.

<h1>HTML Code here</h1>
<?
 print "Hello World\b";
?>

Can anybody advise on which perl templating engine (and possibly configuration) will allow me to code similarly?

3) PHP function library Anybody know of a library for perl which reproduces a lot of the php built-in functions?


Have a look at EmbPerl.

It's a Perl based templating system, which seems to provide anything that PHP does based on my admittedly very small knowledge of PHP.

To cover your specific points:

  • $_GET : EmbPerl provides %fdat hash which contains full set of form data passed via either POST or GET

    %fdat makes no distinction of whether the value originated in GET's query string or form field via POST).

    If you absolutely MUST have only the values from GET's QUERY_STRING, here's a simple example of a function to get it: http://urlgreyhot.com/personal/resources/embperl_getting_values_query_string - although why would you want to separate GET from POST data is escaping me at the moment.

  • $_SESSION : I'm not 100% I get what this does in PHP but if I'm right, there's %udat for per-user data and %mdat for per-module/page data for handling session stuff.

    Using both is described in more detail in "Session Handling" area of EmbPerl docs, along with all the other multitude of session support in EmbPerl

    Here's a quick %udat blurb:

    ... as soon as you write anything to %udat, Embperl creates a session id and sends it via a cookie to the browser. The data you have written to %udat is stored by Apache::Session. The next time the same user request an Embperl page, the browser sends the cookie with the session id back and Embperl fills the %udat hash from Apache::Session with the same values as you have stored for that user.

  • The templating code you included would look like this in EmbPerl:

       <h1>HTML Code here</h1>
       [-
        print OUT "Hello World!";
       -]
    

    Or for a more idiomatic/correct solution,

       <h1>HTML Code here</h1>
       [+ "Hello World!" +]
    

    P.S. I have no clue what "\b" does in PHP so I didn't clone that.

    Embperl supports all the standard templating stuff ([- -] for execution, [+ +] for inclusion of results of arbitrary Perl code, template flow control commands ([$ if $]/'[$ for $]` etc...) and much more. It's also fully compatible with mod_perl.


2) If you literally want your script to be the template as in PHP, there is the Markup::Perl module (which grew out of another project that was actually called PerlHP). There are other modules like HTML::Mason for what Perl programmers think of as templating engines.

3) On CPAN I found PHP::Strings and PHP::DateTime, but I haven't used them and otherwise can't vouch for them.


You should also check out mod_perlite, it's an Apache module trying to emulate the mod_php behaviour for Perl, although development on it seems to have been stalled. More info from the README.


I was going to tell you to love Perl and PHP for their unique selves, but no. 1 strikes me as a bit of idle fun. My advice is to code it yourself, and post it to CPAN. I read your question and thought:

use CGI::PHPLike qw(:superglobals); # Pull in everything from CGI::PHPLike::Vars

CGI::PHPLike::Config->variables_order 'EGPCS';

...

%_ENV is probably just an alias for perl's %ENV. %_REQUEST and %_SESSION are probably tied objects, etc. Heck, %_SESSION may even be backed by PHP::Session::Serializer::PHP.

Read the CGI spec, and check out the source of CGI.pm, of course, but also simpler modules like CGI::Lite.

0

精彩评论

暂无评论...
验证码 换一张
取 消