in reply to need some HTTP::Server::Simple::CGI example

Here's a simple (and hopefully self-explanatory) example:

#!/usr/bin/perl { package MyWebServer; use HTTP::Server::Simple::CGI; our @ISA = qw(HTTP::Server::Simple::CGI); my %dispatch = ( '/hello.cgi' => \&resp_hello, # ... ); 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_hello { my $cgi = shift; # CGI.pm object return if !ref $cgi; my $who = $cgi->param('name'); print $cgi->header, $cgi->start_html("Hello"), $cgi->h1("Hello $who!"), $cgi->end_html; } } # end of package MyWebServer # start the server on port 8080 my $pid = MyWebServer->new(8080)->background(); print "Use 'kill $pid' to stop server.\n";

After having started the script, you should be able to access it via the URL you specified. Good luck.

Replies are listed 'Best First'.
Re^2: need some HTTP::Server::Simple::CGI example
by ww (Archbishop) on Sep 01, 2007 at 15:02 UTC
    ++ almut

    If OP is on windows, kill won't kill because it doesn't exist there; perhaps add:

    print "or, on Windows, stop server with Cntrl-C.\n";
Re^2: need some HTTP::Server::Simple::CGI example
by Anonymous Monk on Feb 13, 2008 at 20:13 UTC
    Almut, Your example really helped me. I was having trouble figuring out how to make H:S:S work. I annotated the H:S:S POD with a link to your post. Would you mind if your example was placed in the POD for H:S:S? Thanks, Mark

      I'm glad it helped, and of course I wouldn't mind if it finds its way into the POD.

        Almut, I asked the author of H:S:S (Jesse Vincent) if he would include your example, and gave him the link to your approval. He was very responsive, releasing 0.29 to include your example (with attribution). http://search.cpan.org/~jesse/HTTP-Server-Simple-0.29/lib/HTTP/Server/Simple.pm Thanks for your generosity! Mark