in reply to CGI aligning

As well as what is mentioned above you can simplify your script like this. By defining our tokens just the once then using map (one option) or loops (another option) we make perl do all the hard work. I have also shown you how to use CGI.pm's table method to wrap the input fields in a table to make them neat and CGI::escapeHTML to escape HTML chars for you. BTW -w and use warnings; do the same thing. You only need 1 or the other but -w is more portable as use warnings is a 5.6+ widget.

#!/usr/bin/perl use strict; use warnings; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; my $adv = "Advertise your script here!"; my $adminmail = "sulfericacid\@qwest.net"; my @tokens = qw( abstract author distributor copyright keywords descri +ption generator robots language distribution rating ); print header, start_html(), start_form, table({-border=>0}, Tr( {-align=>RIGHT,-valign=>TOP}, [ td(['Email Address: ', textfield('usermail')]), (map{ td([ucfirst($_).": ", textfield($_)])} @tokens), td([ '', submit]), ]) ), end_form, hr; my $tags =''; for my $token ( @tokens ) { my $content = CGI::escapeHTML(param($token)); next unless $content; $tags .= qq!&lt;meta name="token" content="$content"&gt;<br>\n!; } print $tags; print hr; print end_html();

A bonus of using tokens as shown is that you will never have typos on your param names as you are using the same names to generate and look at them. Nor will you miss input fields....

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: CGI aligning
by sulfericacid (Deacon) on Apr 03, 2003 at 06:57 UTC
    Thanks for all your help! I knew there was a shorter way to write this, there must be a dozen ways to do it but as for right now I'll continue writing the script til all the features are done before I attempt to rewrite it for it to become shorter. I like your codes only have to be defined once and then they work instead of the three times it takes me, but one problem I see is not all the form fields will be text popup_menus as well and with your setup I'd have to use tokens and go back to using the original method, right?

    Yeah, my code is ugly right now so I better run off to cpan and read up on CGI tables, lol. Thanks again!

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      All you do is have another array of tokens or for something a little more flash use a fields hash:

      #!/usr/bin/perl use strict; use warnings; use CGI ':standard'; my %fields = ( abstract => [], enough => [], drop => [ 'foo', 'bar', 'baz' ] ); print header, start_html(), start_form, table({-border=>0}, Tr( {-align=>LEFT,-valign=>TOP}, [ td(['Email Address: ', textfield('usermail')]), format_input(%fields), td([ '', submit]), ]) ), end_form, hr; sub format_input { my %fields = @_; my @tds; for my $field ( sort keys %fields ) { if ( @{$fields{$field}} ) { my $list = popup_menu( -name => $field, -values =>$fields{$field}, ); push @tds, td([ ucfirst($field).': ', $list ]); } else { push @tds, td([ ucfirst($field).': ', textfield($field)]); } } return @tds; } my $tags =''; for my $token ( keys %fields ) { my $content = CGI::escapeHTML(param($token)); next unless $content; $tags .= qq!&lt;meta name="token" content="$content"&gt;<br>\n!; } print $tags; print hr; print end_html();

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Thanks for the total rewrite of my entire script, and as tempting as it is to just rip it off and call it my own it won't do me any good and I won't get any satisfaction from completing my own script, lol. (watch me get downvoted for joking...I always get downvoted for it).

        Your code is very confusing for me so in all honesty as repetitive writing as my script is right now it's a lot more understandable and more clear to me what it's actually doing. Do you think any experienced programmer would find your script easier to maintain than mine, honestly? It seems really odd to repeat so many things in my script but as of right not it's all I can understand :( Do you know of a good node or tutorial on how to cut back on repetitive codes?

        Thanks for your help!

        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid