in reply to Re: Re: CGI aligning
in thread CGI aligning

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

Replies are listed 'Best First'.
Re: Re: Re: Re: CGI aligning
by sulfericacid (Deacon) on Apr 03, 2003 at 13:01 UTC
    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

      In the first example I showed you how to cut back on the repetition. If you have a list of items to do you typically stick them in a list and use a loop to process them all (exactly the same way). This has a number of advantages. First it is shorter. Next it is consistent - if one is right all are right. Next if you use a list to generate input fields you can use the same list to process them - thus you never forget to process an input field (or have typo) which leaves you scrathing your head when say all your email addresses are blank because you had user_email in the form and param('useremail') in the script for instance. If you are doing the same thing many times you make a sub to do it ie escapeHTML() in CGI.pm which we use or the format function that makes formatted table rows.

      As for a tutorial you just got one!

      No offense but any experienced programmer would wince at your code and find it unmaintainable. Don't worry -> YOU WILL TOO in a few months! Looking at old code often casues feelings of nausea (especially if you wrote it when you were new to the languague). You get better through practice and with experience.

      Just keep coding, ask questions and try to understand code like the example I posted. Use loops and lists if you find yourself doing the same thing over and over. Make subs if you are doing anything more than once. Learn about modules. Use standard modules. Write your own modules (see my module tutorails in Tutorials) and also the CGI Help Guide. map and grep are really useful widgets for CGI and lots of other stuff - learn how they work and be rewarded with hours of saved time and effort. Learn about references and data structures and use them. Data::Dumper will be your friend here.

      cheers

      tachyon

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