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

In the script below I am having trouble printing the form results in a clean alignment. You can view the script at http://sulfericacid.perlmonk.org/tags.pl, fill in all the fields and click submit to see the results.

The first line prints the text <br> rather than printing the line break and on the first line it prints two form elements on the same line (when it should all be broken down on their own line like the rest of the script).

Can someone show me how to break the first line into two lines and remove the <br>?

#!/usr/bin/perl -w use CGI::Carp 'fatalsToBrowser'; use strict; use warnings; my $adv = "Advertise your script here!"; my $adminmail = "sulfericacid\@qwest.net"; use CGI qw/:standard/; print header, start_html(), start_form, "Email Address: ",textfield('usermail'),p, "Author: ",textfield('author'),p, "Distributor: ",textfield('distributor'),p, "Copyright: ",textfield('copyright'),p, "Abstract: ",textfield('abstract'),p, "Keywords: ",textfield('keywords'),p, "Description: ",textfield('description'),p, "Distribution: ",textfield('distribution'),p, "Robots: ",textfield('robots'),p, "Rating: ",textfield('rating'),p, "Language: ",textfield('language'),p, submit, end_form, hr; my $abstract = param('abstract'); my $usermail = param('usermail'); my $author = param('author'); my $distributor = param('distributor'); my $keywords = param('keywords'); my $description = param('description'); my $distribution = param('distribution'); my $robots = param('robots'); my $rating = param('rating'); my $language = param('language'); my $copyright = param('copyright'); my $tags =''; if (param('abstract') ne "") { $tags .= qq(&lt;meta name="abstract" content="$abstract"&gt;<br>) +; } if (param('author') ne "") { $tags .= qq(&lt;meta name="author" content="$author"&gt;<br>); } if (param('distributor') ne "") { $tags .= qq(&lt;meta name="distributor" content="$distributor"&gt +;<br>); } if (param('copyright') ne "") { $tags .= qq(&lt;meta name="copyright" content="$copyright"&gt;<br +>); } if (param('keywords') ne "") { $tags .= qq(&lt;meta name="keywords" content="$keywords"&gt;<br>) +; } if (param('description') ne "") { $tags .= qq(&lt;meta name="description" content="$description"&gt +;<br>); } $tags .= qq(&lt;meta name="generator" content="$adv"&gt;<br>); if (param('robots') ne "") { $tags .= qq(&lt;meta name="robots" content="$robots"&gt;<br>); } if (param('language') ne "") { $tags .= qq(&lt;meta name="language" content="$language"&gt;<br>) +; } if (param('distribution') ne "") { $tags .= qq(&lt;meta name="distribution" content="$distribution"& +gt;<br>); } if (param('rating') ne "") { $tags .= qq(&lt;meta name="rating" content="$rating"&gt;<br>); } $tags =~ s/</&lt;/; $tags =~ s/>/&gt;/; $tags =~ s/<br>//; print $tags; print hr; print end_html();


"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

Replies are listed 'Best First'.
Re: CGI aligning
by jasonk (Parson) on Apr 03, 2003 at 04:22 UTC

    Your script is printing <br> because you explicitly told it to with these two lines:

    $tags =~ s/</&lt;/; $tags =~ s/>/&gt;/;

    And then, right after that you tried to remove the breaks, but s/<br>// isn't going to match anything, because you just removed all the < and > characters from your string.


    We're not surrounded, we're in a target-rich environment!
      Yup, that was the problem. I should have taken out the substitutions but I forgot about it (I was originally using <'s and >'s so I actually had to substitute them). Why would the error only occur on the first line though? The same subsitutions were performed for all the parameters but only one <br> was printing and only the first two tags were on the same line.

      Thanks!

      "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

Re: CGI aligning
by tachyon (Chancellor) on Apr 03, 2003 at 06:35 UTC

    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

      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