tags:

views:

134

answers:

4

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 following 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. i

<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 inbuilt functions?

+5  A: 

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.

mobrule
+2  A: 

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.

kixx
I'm a contributor. There are no further commits from me because the software works for me and therefore I consider it finished. (If it doesn't for you, file a bug.) That often happens with small software, so "stalled" is not necessarily something bad.
daxim
OK, I haven't actually got around to try it yet, but I sure will. You might want to point out on the site that the project is considered stable and ready to use, because the first impression is that the project has been neglected or even abandoned. "It runs Movable Type" and "time to start testing" doesn't mean much to the casual visitor.
kixx
+1  A: 

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.

pilcrow
There wasn't really the answer I was looking for, but the accepted answer came closest. I think what I'm really looking for is a combination ofa) the way PHP mixes html and code on the page, and lets you include other files that can share the same session (as opposed to perl's code centric model)b) $_GET, $_POST, $_SESSION variables.I think i will just have to code this up based on the above answers. Thanks everyone1
Nicholas
+6  A: 

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.

DVK