Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

HTML forms

by Anonymous Monk
on Dec 27, 2003 at 16:30 UTC ( #317196=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Under, is it possible to print HTML forms using straight HTML? Instead of print Tr(td(textbox(->..) do something like:
print <<"END"; <table><tr><td>Name: </td> <td><input type="text" name="name" size="4 +0" /></td></tr></table> END
And still use the textbox name as you normally would (my $data = param('name');) ? What I'm really trying to ask is, is it possible to paste straight HTML forms and have them work?

Comment on HTML forms
Download Code
Replies are listed 'Best First'.
Re: HTML forms
by davido (Archbishop) on Dec 27, 2003 at 16:34 UTC
    Yes. It is. Just DWYM and it will work. HERE docs work fine, intermingled with output from

    Just be sure that the field names you use in your HERE doc are the same ones you look for via $q->param('...'). There is no difference between the output of the " HTML shortcuts" and the output of printing a HERE doc.

    The strongest case for using the HTML shortcuts is that it adds some degree of compiletime syntax checking to your HTML. But frankly, I often find them awkward, and often find myself using only for it's input parameter handling, handling uploads, and for the header() method. YMMV.


      Hi davido,

      But frankly, I often find them awkward, and often find myself using only for it's input parameter handling, handling uploads, and for the header() method. YMMV.
      That's exactly what I'm using for. It kinda makes me wonder if it's because I've yet to fully learn its functionality with respect to outputting HTML. Ah, now I know I'm not alone...:)

      The reasons I shun for its HTML capability are 1) I don't want to have to learn the additional syntax to do HTML and 2) I'm already very familiar with HTML and want to make use of that knowledge.

      Lately, I discovered HTML::Template through this community and have since used it for managing content. I find it a really neat solution to marry the two skills of perl CGI and HTML.

Re: HTML forms
by b10m (Vicar) on Dec 27, 2003 at 16:38 UTC

    Why don't you just try and see wheter it works? But yes, you can print straight HTML.

    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; my $q = new CGI; print header, start_html; if($q->param('name')) { print h1("You becha this works.!"), p("You entered: ". $q->param('n +ame')); } else { print<<END <form action="test.cgi" method="POST"> <table> <tr> <td><input type="text" name="name"></td> </tr> </table> <input type="submit"> </form> END ; } print end_html;

    update: Removed <readmore> tags, for there wasn't that much more to read.

Re: HTML forms
by pg (Canon) on Dec 27, 2003 at 17:00 UTC

    Don't be scared by those core modules. Although probably has more function calls than most of other core modules, and tend to scare people, it is actually nothing more than a bunch of functions, which take parameters and generate HTML format strings. Just as you are able to concatnate HTML strings returned from various function calls, it is quite obvious that you can mix your own HTML format string with them.

Re: HTML forms
by CountZero (Bishop) on Dec 27, 2003 at 20:19 UTC
    Yes, sure you can roll your own HTML.

    If you do that, you do not need the HTML functions of and you can replace by CGI::Simple (less overhead and faster).


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

      I took your advice and plugged CGI::Simple::Standard into a current project and found it consistently about 1/10 to 2/10 of a second slower than standard CGI. Current versions of both modules on activestate. Changing one line changed the output from times():
      #use CGI qw(Vars param header redirect); use CGI::Simple::Standard qw(Vars param header redirect);

      perl -MO=Deparse -e"u j t S n a t o e h r , e p l r a h k c r e"

        Very interesting: CGI::Simple states to be faster than CGI. So either this claim is not true or there is something else: perhaps CGI is preloaded by the server and CGI::Simple needs to be loaded again and again at the start of the script? If I have some time to spare during the next weekend, I might try some tests myself.

        Update: I now only see that you are using the functional interface to CGI::Simple (i.e. CGI::Simple::Standard) and that the claims to be faster only relate to the OO-interface.


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

Re: HTML forms
by CharlesClarkson (Curate) on Dec 28, 2003 at 01:50 UTC

    I would advise you avoid embedded mark up in your scripts if you can. I realize that some situations make this impossible, but one of the hardest things for many web designers to easily edit is perl code.

    I prefer HTML::Template. YMMV. Search CPAN for templates. If you are going to share your code with others or if anyone else is involved with the design of your site, they will thank you for not embedding mark up.

Re: HTML forms
by esharris (Monk) on Dec 28, 2003 at 08:58 UTC
    I started writing CGI scipts using straight HTML. But now I am experimenting with using I suggest buying Lincoln Stein's book. There is a methodology behind that should make CGI scripts easier to build and maintain. The thing that sold me is the sticky forms. If I want to let the user hit a submit button that does something and restores that form without resetting the fields to its default values, I can do it with less explicit code. Instead of reinventing this mechanism, I can use a common convention. One side benefit and that the CGI functions often produces better output. Using straight HTML, I was able to introduce some javascript. Using CGI functions, I was able to do the same thing. However, the CGI functions also added code to comment out the javascript within older browsers. Another side benefit is that the code needed to build tables is more compact when you use CGI functions. I have some colleagues who applaud the move. Conversely, there are a few who make it sound like CGI is going to eventually bite me. We'll see.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://317196]
Approved by Corion
Front-paged by grinder
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2015-08-29 14:06 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (343 votes), past polls