Category: |
Miscellaneus |
Author/Contact Info |
/msg sulfericacid |
Description: |
10-field meta tag generator. Create flashy meta tags on the fly and give the option to have a copy of the tags emailed to the user.
Help! I know this is a very large code with many repetitive lines. I'd like to rewrite this script once more from grounds up to make it smaller but found that is too much work for me to understand right now. Instead can someone show me a way to shrink the my $var = param('name') section? |
#!/usr/bin/perl -w
use CGI::Carp 'fatalsToBrowser';
use strict;
use warnings;
my $adv = "Advertise your script here!"; # Simple text ad for this sc
+ript
my $sendmail = "/usr/lib/sendmail";
my $scriptname = "Meta Tag Generator"; # Name you want to call this
+ script
my $scriptlocation = "http://www.where.com/tags.pl"; # Link send to th
+eir email addy
use CGI qw/:standard/;
print header,
start_html('Meta Tag Generator');
print start_form(), table(
Tr(td( "Email Address: "), td(textfield(-name=>'usermail',
-size=>40)) ),
Tr(td("Author: "), td(textfield(-name=>'author',
-size=>40)) ),
Tr(td("Distributor: "), td(textfield(-name=>'distributor
+',
-size=>40)) ),
Tr(td("Copyright: "), td(textfield(-name=>'copyright',
-size=>40)) ),
Tr(td("Abstract: "), td(textfield(-name=>'abstract',
-size=>40)) ),
Tr(td("Keywords: "), td(textarea(-name=>'keywords',
-rows=>5,
-columns=>30)) ),
Tr(td("Description: "), td(textarea(-name=>'description'
+,
-rows=>5,
-columns=>30)) ),
Tr(td("Robots: "), td(popup_menu(-name=>'robots',
-values=>['','index','noindex','follow','nofollow'])) )
+,
Tr(td("Distribution: "), td(popup_menu(-name=>'distributi
+on',
-values=>['','local','global'])) ),
Tr(td("Language: "), td(popup_menu(-name=>'language',
-values=>['','EN','EN-GB','EN-US','ES','ES-ES','FR','IT
+','JA','KO','DE'])) ),
Tr(td("Rating: "), td(popup_menu(-name=>'rating',
-values=>['','kids','general','mature','restricted']))
+),
Tr(td(), td(checkbox(-name=>'emailme',
-checked=>1,
-value=>'ON',
-label=>'Email my tags!')) ),
Tr(td(), td(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 $emailme = param('emailme');
my $tags ='';
if (param('abstract') ne "") {
$tags .= qq(<meta name="abstract" content="$abstract"><br>)
+;
}
if (param('author') ne "") {
$tags .= qq(<meta name="author" content="$author"><br>);
}
if (param('distributor') ne "") {
$tags .= qq(<meta name="distributor" content="$distributor">
+;<br>);
}
if (param('copyright') ne "") {
$tags .= qq(<meta name="copyright" content="$copyright"><br
+>);
}
if (param('keywords') ne "") {
$tags .= qq(<meta name="keywords" content="$keywords"><br>)
+;
}
if (param('description') ne "") {
$tags .= qq(<meta name="description" content="$description">
+;<br>);
}
$tags .= qq(<meta name="generator" content="$adv"><br>);
if (param('robots') ne "") {
$tags .= qq(<meta name="robots" content="$robots"><br>);
}
if (param('language') ne "") {
$tags .= qq(<meta name="language" content="$language"><br>)
+;
}
if (param('distribution') ne "") {
$tags .= qq(<meta name="distribution" content="$distribution"&
+gt;<br>);
}
if (param('rating') ne "") {
$tags .= qq(<meta name="rating" content="$rating"><br>);
}
if (param()) {
print "<b>Instructions:</b> To install your brand new meta tags copy a
+nd paste your code below between the \<HEAD\> and \</HEAD\> tags of y
+our website!<br><br>";
print $tags;
print hr;
print end_html();
### Sent their tags via email
if ($emailme ne "") {
$tags =~ s/</</g;
$tags =~ s/>/>/g;
$tags =~ s/<br>/\n/g;
open (MAIL, "|$sendmail -t") or die "Cannot access mail";
print MAIL "To: $usermail\n";
print MAIL "From: $adminmail\n";
print MAIL "Subject: Your Meta Tags\n\n";
print MAIL "This is a copy of the meta tags you designed with '$scri
+ptname' recently.\n\n";
print MAIL "To install these please paste these codes inside the HEA
+D tags of your website.\n\n";
print MAIL "$tags\n\n";
print MAIL "To make new meta tags please come back to $scriptlocatio
+n!\n";
close (MAIL);
}
}
|
(jeffa) Re: Meta Tag Generator
by jeffa (Bishop) on Apr 05, 2003 at 21:19 UTC
|
We need to get you into the habit of using arrays to cut
back on redundant code. The following stipped-down version
of your code demonstrates how easy it really is to write
maintainable code. I have left out the mail part completely,
and i am only using textboxes - putting the text areas, and
select boxes back in is your homework assignment. Hope this
gets the lightbulb burning bright in your head. ;)
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI qw(:standard);
my @name = qw(
author distributor copyright
abstract keywords description
distribution robots language rating
);
print header,
start_html('Meta Tag Generator'),
start_form,
table(
# i know maps are scarey, just think of this as a
# backwards for loop (read bottom up)
map {
Tr(
td(ucfirst $_ . ': '),
td(textfield(-name=>$_,-size=>40)),
)
} @name
),
submit('submit'),
end_form,
;
if (param('submit')) {
for (@name) {
# this is how you trim down fetching params code ;)
my $param = param($_);
# this skips empty fields, you might want to err instead
next unless $param;
# notice the use of meta() as well as escapeHTML()
# since we have alread loaded CGI.pm, let's use it!
print escapeHTML(meta{name=>$_,content=>$param}),br;
}
}
print end_html;
So, how would i go about adding specifics to the items, such
as using a text area for description and a drop down box for
language? For the former i would use a config hash, one that
knows which form element to use ... well, a picture is worth
a thousand words ... so ponder on this after you master the
code above:
my @dist = qw(local global);
my @robo = qw(index noindex follow nofollow);
my @lang = qw(EN EN-GB EN-US ES ES-ES FR IT JA KO DE);
my @rate = qw(kids general mature restricted);
my %name = (
author => [ sub{textfield(@_)} , {-size=>40} ],
distributor => [ sub{textfield(@_)} , {-size=>40} ],
copyright => [ sub{textfield(@_)} , {-size=>40} ],
abstract => [ sub{textfield(@_)} , {-size=>40} ],
keywords => [ sub{textarea(@_)} , {-rows=>5,-columns=>30} ],
description => [ sub{textfield(@_)} , {-rows=>5,-columns=>30} ],
distribution => [ sub{popup_menu(@_)} , {-values=>['',@dist]} ],
robots => [ sub{popup_menu(@_)} , {-values=>['',@robo]} ],
language => [ sub{popup_menu(@_)} , {-values=>['',@lang]} ],
rating => [ sub{popup_menu(@_)} , {-values=>['',@rate]} ],
);
print header,
start_html('Meta Tag Generator'),
start_form,
table(
map {
Tr(
td(ucfirst $_ . ': '),
td $name{$_}[0]->( -name=>$_, %{$name{$_}[1]} )
)
} keys %name
),
submit('submit'),
end_form,
;
if (param('submit')) {
for (keys %name) {
my $param = param($_);
next unless $param;
print escapeHTML(meta{name=>$_,content=>$param}),br;
}
}
print end_html;
The idea is to give each key a datastructure that contains
an anonymous subroutine to execute and some arguments to
pass to that subroutine. It is complex, but it is also
terse and easy to maintain. But of course, now that @name is
%name (a hash), the order is not preserved. There are vays
to fix zis, but i'll let you try to figure out how as an
exercise. ;)
Good luck. :)
Update: ahhh, indeed it is Aristotle. Thanks for
watching my back once again. ;) sulfericacid ... do what
Aristotle did, there is no need to be needlessly complex.
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
| [reply] [d/l] [select] |
|
Good job, though in this case the subref approach is needlessly complex.
my $textfield = textfield({-size=>40});
my %name = (
author => $textfield,
distributor => $textfield,
copyright => $textfield,
abstract => $textfield,
keywords => textarea({-rows=>5,-columns=>30}),
description => textfield({-rows=>5,-columns=>30}),
distribution => popup_menu({-values=>['',@dist]}),
robots => popup_menu({-values=>['',@robo]}),
language => popup_menu({-values=>['',@lang]}),
rating => popup_menu({-values=>['',@rate]}),
);
print header,
start_html('Meta Tag Generator'),
start_form,
table(
map {
Tr(
td(ucfirst $_ . ': '),
td($name{$_}),
)
} keys %name
),
submit('submit'),
end_form,
;
Makeshifts last the longest. | [reply] [d/l] |
•Re: Meta Tag Generator
by merlyn (Sage) on Apr 08, 2003 at 04:53 UTC
|
| [reply] |
|
|