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.
<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%fdathash 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%udatfor per-user data and%mdatfor 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
%udatblurb:… 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,
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.