How can I get PHP's (deployment) simplicity but Perl's power?

后端 未结 12 1357
没有蜡笔的小新
没有蜡笔的小新 2021-02-12 15:09

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 wi

相关标签:
12条回答
  • 2021-02-12 15:22

    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.

    0 讨论(0)
  • 2021-02-12 15:23

    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.

    0 讨论(0)
  • 2021-02-12 15:25

    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.

    0 讨论(0)
  • 2021-02-12 15:26

    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.

    0 讨论(0)
  • 2021-02-12 15:26

    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.

    0 讨论(0)
  • 2021-02-12 15:30

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

    0 讨论(0)
提交回复
热议问题