当前位置: 动力学知识库 > 问答 > 编程问答 >

How can I write server side code in Perl for the WWW?

问题描述:

I'm new to Perl programming. How can I use it instead of PHP? In Xampp or PHP we have "htdocs" to store our .php files but how could I save my .pl file?

When I try to open a .pl through localhost the whole file was downloadable, unlike in php when we open a .php, the php code is executed.

网友答案:

The simplest option is to configure your web server to support CGI and then write your Perl to follow the CGI protocol (the CGI module is helpful for this). You can configure servers to execute any file with a .pl extension if you wish.

Slightly more complicated, but more flexible, is PSGI/Plack. This is a set of middleware that you can hook up to a web server in a variety of ways (e.g. FastCGI or mod_perl) for better performance. This also gives some nice debugging options. There is an advent calendar style guide that you might find useful.

网友答案:

XoR has already mentioned it, but I want to reiterate: Mojolicious! It has a templating engine that lets you intermix Perl code in the HTML, however you can do most of the heavy lifting inside the application itself. Mojolicious also includes (several) built-in web servers (one for testing, one for production), which are far easier to use than Apache.

Easy to install: just install Mojolicious using cpan (or other).

Write a basic script: ( a simple Hello World ):

#!/usr/bin/env perl

use strict;
use warnings;

use Mojolicious::Lite;

get '/(:name)' => {name => 'World'} => sub {
  my $self = shift;
  $self->render('index');
};

app->start;

__DATA__

@@ index.html.ep
<!DOCTYPE html><html>
<head>
  <title> Hello <%= $name %> </title>
</head>
<body>
  Hello <%= $name %>
</body>
</html>

And run: morbo myapp.pl

网友答案:

From a naive approach, it sounds like your web server isn't configured properly; it should be interpreting the file, not handing it out.

Out of curiosity, why are you using Perl for web stuff if you know PHP? I thought I read an article a while back stating that PHP was faster than Perl when used for displaying webpages.

网友答案:

See "Configuring Apache" on Strawberry wiki.

网友答案:

If you want to start with Perl5 web development it is usefull to start with CGI, but that is not necesity.

There is CGI module. You could look at how HTTP works and how Apache or whatever server you use calls your CGI program. Good start is using this script:

#!/usr/bin/env perl                                                             

use strict;                                                                     
use warnings;                                                                   

print "Content-type: text/html\n\n";                                            
print "<html>";                                                                                           
print "<body>";                                                                 
print "<table>";                                                                
for my $var_name (keys %ENV) {                                                  
  print "<tr>";                                                                 
  print "<td>$var_name</td>";                                                   
  print "<td>$ENV{$var_name}</td>", "\n";                                       
  print "</tr>"                                                                 
}                                                                               
print "</table>";                                                               
print "</body>";                                                                
print "</html>";

Then looking at variables like "REQUEST_URI", "REQUEST_METHOD" and "QUERY_STRING", you can get idea what CGI module is doing in background.

It's important to know that your script must be at directory that is set up to run cgi scripts, like:

ScriptAlias /cgi_bin/ /var/cgi_bin/                                             

<Directory /var/cgi_bin/>                                                       
  Options +ExecCGI                                                                                        
  AddHandler cgi-script .cgi .pl                                                
</Directory>

And file have to be executable by any user that runs your server (eg. Apache2), and readable.

Also after this you can look at modern toolkits like Catalyst & Mojolicious. I recomend you mojolicious. Also there is web framework called Dancer.

About frameworks, Catalyst is long time here and it works, but it kind of took some kraft with it. This is tried to be fixed with new frameworks like mojolicious. Mojolicious don't have almost no external CPAN dependencies.

分享给朋友:
您可能感兴趣的文章:
随机阅读: