photo
Jordan Sissel
geek

Tue, 23 Dec 2003

perl's CGI module

I have overlooked some of the coolest parts of perl's CGI module for the longest time - I've known it can generate HTML but I've always shrugged it off as thinking it can't do what i want it to, i couldn't have been more wrong.

What I'm specifically referring to is form generation. The rest of the HTML generating methods are useless to me (like the h1() method), but I'll focus on the cool form generating ones.
Why are they cool? They simplify your life and make code cleaner - For instance, lets say oyu have this fugly line of code:

$poop = "email_address";
$default = "user@somedomain.com"
print 'Email address: <input type="text" name="' . $poop .  
      '" value="' . $default . '"><br>';

This prints out happy html that looks like this:
Email address:

Now, that print line is pretty damn ugly, but we can easily make it pretty once more by doing:
use CGI qw/:standard/;
print "Email Address: " . textfield($poop, $default) . "<br>";

And the output is:
Email Address:

Neat? Yeah.. What next? More neatness! You have an array you want to turn into a group of checkboxes...

use CGI qw/:standard/;
@foo = ("check1", "foo", "bar", "baz", "poopie", "hello", "Coke!", "Pepsi?");
@def = ("foo", "bar", "poopie");
print 'Checkbox listing:<br>';
print checkbox_group(-name => "options", -values => \@foo, 
                     -columns => 3, -defaults => \@def);
print '<br>';
What does this look like?

Checkbox listing:
check1bazCoke!
foopoopiePepsi?
barhello

Comments: 0 (view comments)
Tags:
Permalink: /geekery/88
posted at: 20:14


0 responses to 'perl's CGI module'


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment:


Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '08 Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials SSH Tunneling (Video)

Tag Cloud

Calendar

< December 2003 >
SuMoTuWeThFrSa
  1 2 3 4 5 6
7 8 910111213
14151617181920
21222324252627
28293031   

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati