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

Hello i am a newbie to perl. quite not sure about the CGI of perl. What excatly we want to display in the html forms.
#!/usr/bin/perl -w print "Content-type: text/html\n\n"; print "<html><head><title>Hello World</title></head>\n"; print "<body>\n"; print "<h2>Hello, world!</h2>\n"; print "</body></html>\n";
doesn't show anything as webpage.

Replies are listed 'Best First'.
Re: perl - html
by McDarren (Abbot) on Jul 11, 2006 at 06:06 UTC
    Your code works fine for me.

    Have you checked that the script is executable, and that your webserver is correctly configured to execute CGI scripts?

    That aside, if you intend to start writing CGI scripts with Perl, then you'll be wanting to learn all about the CGI module. A good starting point for beginners is ovid's CGI course

    Good luck!
    Darren :)

Re: perl - html
by aufflick (Deacon) on Jul 11, 2006 at 06:12 UTC
    Looks ok to me. If you have commandline access on the webserver try executing it on the commandline. If it works as you expect then that eliminates a whole host of problems (like bad line endings, not being flagged as executable etc.). If it's a Windows server then there are even more things to consider.

    I'd also echo the previous poster, to use CGI to simplify your interactions with http and html. Even better, use something like Template::HTML or HTML::Mason.

    Here is your example using CGI in OO mode:

    use strict; use CGI; my $q = new CGI; print $q->header; print $q->start_html('Hello World'); print $q->h2('Hello, world!'); print $q->end_html;
Re: perl - html
by cwry (Monk) on Jul 11, 2006 at 05:59 UTC

    Your script works for me as it is, so it sounds like you have a configuration issue with your web server.

    Have you checked to see if there's any output at all? Are you getting an error instead of the page you expected? Does the web server log anything pertaining to the request?

    As a side note, not that this has anything to do with your problem, you should consider using the CGI module instead of printing the raw header + html yourself.

      Hi Thank you
      "Content-type: text/html <html><head><title>Hello World</title></head> <body> <h2>Hello, world!</h2> </body></html>
      This was my output which i have got in command line. its not comming in the webpage.

        Hi,

        The output of your script is fine. The problem is that your web server is not executing the script properly when you access the page. Can you provide more information such as looking at the webserver logs? Have you successfully run other CGI scripts? Is the script set to executable as McDarren suggested? Are you getting any errors?

Re: perl - html
by CountZero (Bishop) on Jul 11, 2006 at 08:49 UTC
    Are you running this on a web-server at all or just at the command-line?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law