"Hello World" in Perl

During "response phase" dynamic document is created and sent to browser. This is also when CGI scripts run.

The Simple Way

Put Apache::Registry in charge of your cgi-bin directory.

All CGI scripts will now load much faster. Example for srm.conf:

<Location /cgi-bin>
  SetHandler  perl-script
  PerlHandler Apache::Registry
  PerlSendHeader On
  Options +ExecCGI
</Location>

The Powerful Way

Entry for srm.conf:
 <Location /hello_perl>
    SetHandler  perl-script
    PerlHandler Apache::Hello
  </Location>

Script III.1.1 Apache::Hello

package Apache::Hello;
# file: Apache/Hello.pm

use strict vars;
use Apache::Constants ':common';

sub handler {
    my $r = shift;
    $r->content_type('text/html');
    $r->send_http_header;
    return OK if $r->header_only;

    my $remote_host = $r->get_remote_host;
    my $user_agent  = $r->header_in('User-Agent');

    $r->print(<<END);
<html>
<header>
<title>Greetings</title>
</header>
<body>
<h1>Greetings from Perl</h1>
<p> Hello to you, <b>$remote_host</b>.</p>
<p> You are the lucky user of a <i>$user_agent</i></p>
<hr>
END

   $r->printf("<p>Path info = <b>%s</b></p><hr>\n",$r->path_info)
         if $r->path_info;
   $r->printf("<p>Query = <b>%s</b></p><hr>\n",$r->args)
         if $r->args;

print <<END;
</body>
</html>
END

    return OK;
}
1;

What it Looks Like

http://localhost/hello_perl/foo/bar?baz
<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Thu Nov 4 07:12:50 EST 1999