#!/usr/bin/perl { package MyWebServer; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); my %dispatch = ( '/bla' => \&resp_bla, ); sub handle_request { my $self = shift; my $cgi = shift; my $path = $cgi->path_info(); my $handler = $dispatch{$path}; if (ref($handler) eq "CODE") { print "HTTP/1.0 200 OK\r\n"; $handler->($cgi); } else { print "HTTP/1.0 404 Not found\r\n"; print $cgi->header, $cgi->start_html('Not found'), $cgi->h1('Not found'), $cgi->end_html; } } sub resp_bla { my $cgi = shift; # CGI.pm object return if !ref $cgi; print $cgi->header("text/plain"), (rand(10) < 5) # pick a random number and see if it is less than 5... ? 1 : 0; #...and print 1 if it is, or 0 if it isn't. } } # start the server on port 8080 # access me at http://localhost:8080/bla my $pid = MyWebServer->new(8080)->background(); print "Use 'kill $pid' to stop server.\n";