Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Problems printing to browser

by frankus (Priest)
on Nov 29, 2002 at 11:42 UTC ( #216502=note: print w/ replies, xml ) Need Help??


in reply to Problems printing to browser

Just a point of preference but:
Try replacing:

my $tags =''; if ($form{'abstract'} ne "") { $tags .= qq(<meta name="abstract" content="$form{'abstract'}"><br +>\n); } if ($form{'author'} ne "") { $tags .= qq(<meta name="author" content="$form{'author'}"><br>\n) +; } if ($form{'distributor'} ne "") { $tags .= qq(<meta name="distributor" content="$form{'distributor' +}"><br>\n); } if ($form{'copyright'} ne "") { $tags .= qq(<meta name="copyright" content="$form{'copyright'}">< +br>\n); } if ($form{'keywords'} ne "") { $tags .= qq(<meta name="keywords" content="$form{'keywords'}"><br +>\n); } if ($form{'description'} ne "") { $tags .= qq(<meta name="description" content="$form{'description' +}"><br>\n); } $tags .= qq(<meta name="generator" content="SpyderTagV1.0!"><br>\n); if ($form{'robots'} ne "") { $tags .= qq(<meta name="robots" content="$form{'robots'}"><br>\n) +; } if ($form{'language'} ne "") { $tags .= qq(<meta name="language" content="$form{'language'}"><br +>\n); } if ($form{'distribution'} ne "") { $tags .= qq(<meta name="distribution" content="$form{'distributio +n'}"><br>\n); } if ($form{'rating'} ne "") { $tags .= qq(<meta name="rating" content="$form{'rating'}"><br>\n) +; } print my $cgi->escapeHTML( $tags);

With:
print my $cgi->escapeHTML( join '', ( map{ defined($form{$_}) ? qq(<meta name="$_" content="$form{$_}"><br> +\n):'' } qw ( abstract author distributor copyright description robots language distribution rating ) ),'<meta name="generator" content="SpyderTagV1.0!"><br>\n' );

Explanation This uses the functional programming idiom, so start from the inner-most loop and work back.

  • Use the qw function to produce an array of string items for everything in brackets. In this case a list of all the names of the elements from the %form hash
  • Use the map function to process each element of the array produced by 'qw'. Map makes a list of all the results in order.
  • Using a ternary operator to return a value: the string if the form item is defined, no text if not.
  • Join all the array elements from the 'map' function. Also join the string outside the brackets for the map function.
  • Finally print the escapeHTML'd version of the returned string from the join.
Other "useful" notes:
  • Use single quotes where possible, ie where variables don't need to be interpolated.

One of the first things I learnt in Perl was to recognise repetitive patterns and then get rid of them.
I'm lazy.
A by product is if you want to add more meta fields it's easier now :)

--

Brother Frankus.

¤


Comment on Re: Problems printing to browser
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://216502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2015-07-31 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls