I named the file pizza.pl. Where do I place it so that when I type http://localhost/pizza.pl, the server knows where to locate the file? With apache, I indicate the location of the files via the httpd.conf. Btw, the content of pizza.pl is as follows (from
GUI with HTTP::Daemon):
use strict;
use HTTP::Daemon;
use HTTP::Status;
use CGI::Simple;
sub res {
HTTP::Response->new(
RC_OK, OK => [ 'Content-Type' => 'text/html' ], shift
)
}
my %pages = (
index => sub {
res qq[
<html><body>
<h1>Pizza cost calculator</h1>
<form method=post action=calc>
Total cost: <input name=cost><br>
Number of eaters: <input name=ppl><br>
<input type=submit value=Submit>
</form>
</body></html>
];
},
calc => sub {
my ($request) = @_;
my $cgi = CGI::Simple->new( $request->content );
res sprintf q[
<html><body>
<h1>Result</h1>
Cost per eater: %.2f<br>
<a href=index>Again!</a>
</body></html>
], $cgi->param('cost') / $cgi->param('ppl');
},
);
my $daemon = HTTP::Daemon->new(LocalAddr => '127.0.0.1') or die;
my $ppid = $$;
if (my $pid = fork) {
while (my $client = $daemon->accept) {
while (my $request = $client->get_request) {
my ($page) = $request->url->path =~ m[^/(\w+)$];
$page ||= 'index';
if (exists $pages{$page}) {
$client->send_response( $pages{$page}->($request) );
} else {
$client->send_error(RC_NOT_FOUND);
}
}
$client->close;
}
} elsif (defined $pid) {
my $url = $daemon->url;
my @browsers = qw(firefox konqueror mozilla opera start);
{
@browsers or die "Couldn't start a browser.";
my $b = shift @browsers;
my $r = system("$b $url");
redo if $r;
}
} else {
die "Couldn't fork!";
}