views:

954

answers:

11

I despise the PHP language, and I'm quite certain that I'm not alone. But the great thing about PHP is the way that mod_php takes and hides the gory details of integrating with the apache runtime, and achieves CGI-like request isolation and decent performance.

What's the shortest-distance approach to getting the same simplicity, speed and isolation as PHP's runtime environment, with Perl semantics? I feel like raw mod_perl gives me too much rope to hang myself with: cross-request globals, messy config, too many template engines to choose from.

FastCGI? HTML::Mason? I'd like to do development largely in Perl, if only I had a framework that let me.

+6  A: 

I'd recommend Catalyst with FastCGI. Also, for templating, Template::Toolkit is my personal favorite, but HTML::Mason is also highly regarded in the community.

Adam Bellaire
+15  A: 

Look at Catalyst this MVC (model, view, controller) framework works stand-a-lone or with apache_perl and hides a lot of the messy bits. There is a slightly odd learning curve (quick start, slower middle, then it really clicks for advanced stuff).

Catalyst allows you to use Template Toolkit to separate the design logic from the business logic, Template toolkit really is great, even if you decide not to use Catalyst then you should be using this. HTML::Mason isn't something I personally like, although if you do all the HTML yourself then you might want to review Template::Declare which is another alternative you can also use with Catalyst.

For database stuff look at DBIx::Class, which yet again works with Catalyst or on it's own.

Ranguard
You could use other template modules.
Brad Gilbert
Catalyst is the nearest to what I really want, although it's not exactly what I asked for :)Sounds like PerLite will do the trick if it ever launches.
djsadinoff
+4  A: 

There are a lot of possibilities, depending on what you want to do.

If you want to take advantage of the speed of mod_perl, but the simplicity of vanilla CGI, check out the Modperl::Registry distribution from CPAN. This will allow you to run your plain CGI scripts largely unaltered.

In terms of frameworks, I'm a big fan of CGI::Application. It provides a very simple inheritance-based framework that handles most everything a web application will need to do, giving you the freedom to design your application the way you like. A simple app can be done in a monolithic fashion; a more complex one can use a full-fledged MVC design. Like Perl in general, CGI-App gives you a lot of options and generally stays out of your way.

CGI-App supports the excellent HTML::Template module by default, and has plugins for other templating systems such as the spectacular Template Toolkit. There are also a plethora of plugins for other purposes.

If you want more work done for you, check out Catalyst. This way of doing things may be more familiar if you've used Ruby on Rails.

Other popular web-app frameworks include Jifty and CGI::Prototype, written by Randal Schwartz, which is based on the Class::Prototyped object framework.

friedo
+3  A: 

The aforementioned Catalyst is a fine tool for constructing entire web applications, but it is by no means anywhere near simple. The primary strength of PHP is that you can embed small chunks of it as needed in otherwise static pages, i.e. you can do:

<html> <body> <p>The value of 2+2 is: <?php echo 2+2; ?></p> </body></html>

and see on your web browser:

The value of 2+2 is: 4

If you try to do something like this with Catalyst (as far as I know), you're developing an entire application with multiple files to print a simple value. At least, there's no explanation of how to do simple embedding in the tutorials that I saw.

Fortunately, this level of simplicity can be reached with Mason, which in some ways (thanks to the power of Perl) can be even simpler. The above example reads:

<html><body><p>The value of 2+2 is: <% 2+2 %></p></body></html>

and you get the same result.

There's no reason you can't start by installing and working with Mason and then install Catalyst side by side with it, however, if you plan to move to very complex, purely Perl-driven projects later, though.

Zed
[% value = 2 + 2 %]The value of 2+2 is [% value %]
Ranguard
You could set the `TAG_STYLE` option to asp for `<% 2+2 %>` or even php for `<? 2+2 ?>`. You even have the option to set your own start, and end tags.
Brad Gilbert
... in Template Toolkit.
Brad Gilbert
+2  A: 

I wonder what became of mod_perlite, which was going to provide exactly what you are after.

Aristotle Pagaltzis
+3  A: 

Stuff like Catalyst and CGI::Application are more equivalents of Zend Framework rather than PHP itself. In order to replicate the basic functionality for creating web pages that PHP offers "out the box" then you need two CPAN modules that should be available in every base Perl installation:

use CGI;
use DBI;

Is all you really need. Now instead of:

$_POST['param']
$_GET['param']

you have:

my $q = new CGI;
$q->param('param'); # same for post or get

And instead of:

$dbh = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$sth = mysql_query("SELECT 1 FROM table", $dbh);
while($row = mysql_fetch_assoc($sth))
{
    // do something with $row
}

You have:

my $dbh = DBI->connect('DBI:mysql:host=localhost;', 'mysql_user', 'mysql_password');
my $sth = $dbh->prepare("SELECT 1 FROM table");
$sth->execute();
while(my $row = $sth->fetchrow_hashref)
{
    # do stuff with row
}

The DBI code is slightly more complicated because it offers prepared statments and bound variables so that you don't need to worry about SQL injections. PHP doesn't offer this so you need to use something like PDO or write your own database class.

The only thing left is if you wanted HTML output in a script. But you don't want that do you? You use HTML::Template or Template::Toolkit for that, the same way you should be using Smarty or native templates in PHP.

David McLaughlin
I think the bottom line is that FCGI needs to be sprinkled on top of this before it's really a plausible design pattern, especially from a performance perspective.
djsadinoff
+5  A: 

The closest, well-regarded equivalent to PHP in Perl is probably HTML::Mason.

Like PHP, it embeds Perl into your document and renders it:

% my $noun = 'World';
Hello <% $noun %>!
How are ya?

The O'Reilly book Embedding Perl in HTML with Mason is available online for free.

xdg
I don't see mixing code and HTML as a good point - it soon leads to a mess.
David Precious
@David, the opportunity to abuse this is quite tempting. It is nice not to have to learn a whole new syntax to handle flow control and data access though. IMO, the really exciting things with Mason are the autohandlers, dhandlers and component inheritance. But they aren't really like anything I know of in PHP.
daotoad
A: 

I agree with Aristotle. mod_perlite sounds like just what you are looking for, if only it was finished.

A: 

I've worked with HTML::Mason, first hacking RT and then creating two sites with it. There's a learning curve, but it's not too bad. Worse, I think, is installing the thing, but that has much more to do with Apache and mod_perl than Mason. Once the pieces are in place, it's only as complicated as you make it (like Perl itself).

Joe Casadonte
+1  A: 

The closest to PHP in terms of simplicity is HTML::Mason.

Suggesting Catalyst is a bad joke for someone who is looking for simplicity... And I'm happily working with Catalyst every single day now.

melo
+2  A: 

I just saw Dancer. Looks like this might be a good option.

djsadinoff