bkiahg has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks,

UPDATE: Figured it out. Explanation below:

I am having a bit of a problem running a simple webserver on my local machine. Everything seems to be working fine except when I try and print out a simple message to the browser it dies on me. Any ideas on what could be causing this?
use HTTP::Daemon; use HTTP::Status; my $d = HTTP::Daemon->new(Listen => 5, LocalAddr => 'localhost', LocalPort => 80, Proto => 'tcp') or die "Can't bin +d : $@\n"; print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET') { # dies right on this line $c->send_response("Content-type: text/html\n\nHello Worl +d"); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }
It's very possible I am misunderstanding the send_response function or how to interact with it.

Thank you in advance.

UPDATE: Figured it out, needed to add the Content-type: text/html\n\n to the output. My mistake. Updated the code so that anyone who finds it has working code.

UPDATE2: In playing with HTML::Daemon below is better code to work with it. Just FYI:
use HTTP::Daemon; use HTTP::Status; require 'C:\\rrships\\default.cgi'; my $d = HTTP::Daemon->new(Listen => 5, LocalAddr => 'localhost', LocalPort => 80, Proto => 'tcp') or die "Can't bin +d : $@\n"; # print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { # print $r->url->path . "\n"; if ($r->method eq 'GET') { my $response = HTTP::Response->new(200); $response->content(&Hello_World($r->url->path, $r->url-> +query)); $response->header("Content-Type" => "text/html"); $c->send_response($response); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }

Replies are listed 'Best First'.
Re: HTTP::Daemon send_response Problem
by talexb (Chancellor) on Dec 29, 2008 at 08:53 UTC

    As a note, you don't need to undef $c at the location you're doing it -- $c will fall out of scope naturally at the end of the loop. That's the more Perl-ish way (I think) to do things.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: HTTP::Daemon send_response Problem
by afresh1 (Hermit) on Dec 29, 2008 at 21:53 UTC

    I actually think you will probably want something more like this:

    if ($r->method eq 'GET') { $c->send_response('Content-Type: text/html'); print $c "<html><head></head><body>Hello World</body></html>"; }
    I would have expected a more details from the HTTP::Daemon docs, but perhaps they expect you to look at the examples.

    l8rZ,
    --
    andrew