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

folks , I am so new to CGI and trying a simple example here is what I have :
<html> <head> <title>Title of page</title> </head> <body> This is my first homepage. <b>This text is bold</b> </body> </html>
and trying to calling it from a perl script :
#!/usr/bin/perl -w use strict; use CGI qw( :standard ); print redirect( "./cgiSample.html" ) unless ( "name" ); print hr(), end_html();
when I run the html code from the browswer , it works fine, however , when I run the perl scripts , nothing happens ,, all I get is the following on the command line and the browser wouldn't open :
$ perl cgiSample.pl <hr /> </body> </html>
Can someone advice please ?

Replies are listed 'Best First'.
Re: CGI example
by meraxes (Friar) on Nov 26, 2007 at 17:55 UTC

    You're mixing your metaphores, so to speak. :)

    The redirect function won't work at the command line. I'm surprised you're not actually seing the headers for this in the output.

    The only output you are getting is the print hr(),end_html(); because redirect doesn't "load" the page for you, it outputs headers that do a redirect and it can't do a redirect at the command line.

    You cannot both redirect and output HTML at the same time.

    I'd recommend reading a bit more about CGI and perhaps looking into CGI::Application for writing an application.

    --
    meraxes
Re: CGI example
by bradcathey (Prior) on Nov 26, 2007 at 20:14 UTC

    From the looks of your code, I'm really not too sure what you are trying to do, and if CGI is really necessary.

    IMHO, the first question you should ask yourself is "why do I need to use CGI?" I.e., what are you trying to accomplish by using it? As a web developer myself, I use it for 1) on a simple level: processing forms, and 2) on a more complex level: building out database-driven, content-managed sites.

    When I first started programming in Perl/CGI, I wanted to process forms, e.g., a contact form. This meant validating the input, sticking the input into a database, sending a notification email, and finally returning a thank-you page (one of the most recommended of many of the Tutorials is the venerable Ovid's CGI Course).

    As I progressed, I naturally moved into templating (HTML::Template is one of the post-popular and easy to use) and finally frameworks (meraxes mentioned CGI::Application, which is my tool of choice for the heavier lifting).

    Depending on your goal, you may not need CGI at all (unless for a purely academic exercise). Make sure the tool matches the task and that you're not using a 20oz sledge for finishing nail.

    Again, check out the Monasteries great Tutorials.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: CGI example
by locked_user sundialsvc4 (Abbot) on Nov 26, 2007 at 20:24 UTC

    (smile...)

    Think about it:   what does “a CGI application” actually do?

    Answer:   it generates HTML-output, which is then (somehow) sent back to the user's web-browser as input so that the web-browser will now interpret that HTML and make a pretty picture.

    (Let me clarify this:   someone else (Apache...), not the CGI-application, is responsible for “getting the output back to someone's web-browser.” Whatever the CGI-program may print gets captured, and that becomes what gets sent back... but the CGI-program, itself, doesn't do that part.)

    Run a CGI-program from the command-line and what do you get? HTML! But that HTML isn't being fed into anything; there's no browser involved to take that HTML and act upon it. All you get is... HTML, printed on the console.