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

后端 未结 12 1340
没有蜡笔的小新
没有蜡笔的小新 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: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.

提交回复
热议问题