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




Hi Perl Monks,

I really tried to figure this out, but am just stuck. This is for a class and I actually do very little cgi perl.

Anyway, I am just trying to understand how to produce html code, in perl, that succesfully sets the location of objects.

Here is some example html:

<html> <head><title>Message Main Menu</title></head> <h1><font color=green>Message Main Menu</font></h1> <body> <form action="../cgi/messages.pl" method=post> <input type=submit name=query_messages value = "Query Messages" s +tyle="position:absolute; left:80 width: 20em"><br><br> <input type=submit name=add_message value = "Add Message" style=" +position:absolute; left:80 width: 20em"><br><br> <input type=submit name=modify_message value = "Modify Message" s +tyle="position:absolute; left:80 width: 20em"><br><br> <input type=submit name=delete_message value = "Delete Message" s +tyle="position:absolute; left:80 width: 20em"> </form> </body> </html>


There are spots where I want to return to this form and I am using perl. Here is an example of the perl:

sub main_menu { print "<html>"; print "<head><title>Message Main Menu 2</title></head>"; print "<h1><font color=green>Message Main Menu 2</font></h1>"; print "<body>"; print "<form action=\"../cgi/messages.pl\" method=post>"; print "<input type=submit name=query_messages value = \"Query Mess +ages\" style=\"position:absolute; left:80 width: 20em\"><br><br>"; print "<input type=submit name=add_message value = \"Add Message\" + style=\"position\:absolute; left\:80 width\: 20em\"><br><br>"; print "<input type=submit name=modify_message value = \"Modify Mes +sage\" style=\"position\:absolute; left\:80 width\: 20em\"><br><br>"; print "<input type=submit name=delete_message value = \"Delete Mes +sage\" style=\"position\:absolute; left\:80 width\: 20em\">"; print "</form>"; print "</body>"; print "</html>"; }


AND IT JUST DOESN'T DO IT!

I even wrote this snippet with a file handle and looked at the output. It looks IDENTICAL to the html code.

What am I missing? How can the perl produce the identical output, NOT cause the submit objects to have their locations and sizes defined by the code, but the html (on its own) does cause the objects to be located and sized as defined?

And if this just doesn't work, what is an alternative approach?

Really dismayed,

Tony

Replies are listed 'Best First'.
Re: Stuck on some real basic cgi perl
by philcrow (Priest) on Apr 12, 2007 at 15:02 UTC
    What am I missing?
    The short answer is headers. Usually people use CGI to handle details like that. It can also make the generation of html less painful.

    If you don't want to use that module, you must print a header. Add this before the print in your sample above:

    print "Content-type: text/html\n\n";
    Phil
    The Gantry Book is now available.
      Hey Phil,

      No, it wasn't the header! I was getting the objects, it was just that my location code wasn't doing anything.

      I suppose my code looked misleading. The header statement was outside of the subroutine.

      But, thanks!
Re: Stuck on some real basic cgi perl
by Joost (Canon) on Apr 12, 2007 at 15:06 UTC
    Unless you've got more information than just "it doesn't do it", your problem is probably that you don't send out (the right) headers.

    Additional note; if you want to just print big wads of HTML, using multiple print() statements is inefficient, ugly and hard to maintain. Use heredocs instead:

    print <<ENDHTML; # print everything up until the marker <html> <head><title>Message Main Menu</title></head> <h1><font color=green>Message Main Menu</font></h1> <body> <form action="../cgi/messages.pl" method=post> <input type=submit name=query_messages value = "Query Messages" s +tyle="position:absolute; left: +80 width: 20em"><br><br> <input type=submit name=add_message value = "Add Message" style=" +position:absolute; left:80 wid +th: 20em"><br><br> <input type=submit name=modify_message value = "Modify Message" s +tyle="position:absolute; left: +80 width: 20em"><br><br> <input type=submit name=delete_message value = "Delete Message" s +tyle="position:absolute; left: +80 width: 20em"> </form> </body> </html> ENDHTML
    templates are usually even better.
      Hey Joost, I replied to Phil about the headers, wasn't that. I was missing a semicolon in the style statement, so, it was the html.

      Thanks for that more streamlined print method!

      Tony
Re: Stuck on some real basic cgi perl
by logie17 (Friar) on Apr 12, 2007 at 15:54 UTC
    Tony, Are you receiving any error messages from the server? You may need to print a html header before the html output, such as:
    print "Content-Type: text/html; charset=ISO-8859-1\n\n";

    Hope that leads you in the right direction.

    Thanks!
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
      Hey, logie, Nope, that wasn't it (see my other replies), but thanks!

      Tony