Re: any pointers please? combine words script
From: steve_f (me_at_example.com)
Date: 08/03/04
- Next message: Brian McCauley: "Re: any pointers please? combine words script"
- Previous message: Randal L. Schwartz: "Re: creating shell scripts using #!/usr/local/env perl"
- In reply to: Uri Guttman: "Re: any pointers please? combine words script"
- Next in thread: Glenn Jackman: "Re: any pointers please? combine words script"
- Reply: Glenn Jackman: "Re: any pointers please? combine words script"
- Reply: Brian McCauley: "Re: any pointers please? combine words script"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 03 Aug 2004 12:32:26 -0400
Hey Uri...this is what I've got so far. You will notice I am trying to
write more social code ;-)
#!/usr/bin/perl -Tw
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
# This script is a cgi application which
# combines words using either one, two
# or three boxes. The idea was inspired
# by using AdWords, which I must admit
# I am a bit tired of marketing and all
# the consumerism on the internet. But it
# did serve as a useful learning vehicle
# Generous help provided by Uri and Brian
# see the online demo at http://www.combinewords.com/in_progress.cgi
# current as of August 8, 2004
# script is in development, so not all functions
# have been realized yet
# what I am trying to do is develop a cgi framework
# gather all the specific info in one place
# the html in another place
# have the ability to get multiple textareas
# and checkboxes
# maintain user state
# etc
# and then reuse the script for rapid development
# of other ideas ;-)
# by the way, I don't know CGI.pm so well, so
# pointers are appreciated
# OK...here we go...
print "Content-type: text/html\n\n";
# grabs the query string to see what
# mode we are in. if there is no
# query string, sets default to mode 2
my $query = $ENV{QUERY_STRING};
if ($query =~ /boxes=(\w+)/) { $query = $1 }
$query ||= 2;
my %modes = (
1 => {
textareas => [ 'kw_1'],
options => [ 'no_quotes', 'quotes', 'brackets', 'skip_space' ],
submit => 'combine one',
},
2 => {
textareas => [ 'kw_1', 'kw_2' ],
options => [ 'no_quotes', 'quotes', 'brackets', 'reverse_words', 'skip_space' ],
submit => 'combine two',
},
3 => {
textareas => [ 'kw_1', 'kw_2', 'kw_3' ],
options => [ 'no_quotes', 'quotes', 'brackets', 'reverse_words', 'skip_space' ],
submit => 'combine three',
}
);
my $html = get_html(); # get html as a string here
# and then do some substitutions on it
# depending on user choice and input
$html =~ s/%title%/$modes{$query}{submit}/;
$html =~ s/%menu%/join " | \n", get_menu($query, "1::combine.cgi?boxes=1",
"2::combine.cgi?boxes=2",
"3::combine.cgi?boxes=3")/e;
$html =~ s/%number%/$query/;
$html =~ s/%textboxes%/join '<br>', get_textareas($query)/e;
$html =~ s/%checkboxes%/join '<br>', get_checkboxes($query)/e;
$html =~ s/%submit%/$modes{$query}{submit}/e;
# more substitutions
# if user inputed keywords
# triggers script manipulations
# for output
#
# also maintaining state by
# filling in boxes and checking
# the checkboxes
if (param()) {
$html =~ s/%output%/join "<br>\n", do_work()/;
$html =~ s/%user_input%/param(kw_1)/g;
$html =~ s/%checked%//g;
} else {
$html =~ s/%output%//;
$html =~ s/%user_input%//g;
$html =~ s/%checked%//g;
}
print $html;
exit (0);
# swap values in the html below are:
# %title% %menu% %number% %textboxes%
# %checkboxes% %submit% %output%
# other swap vaules embedded in functions
# below are:
# %user_input% %checked%
sub get_html {
return '<html>
<head>
<title>
%title%
</title>
</head>
<body>
<table align="center" width="90%">
<tr align="left" valign="middle">
<td valign="top" width="35%">
<b>Combine words</b><small> - choose number of boxes: %menu%</small>
<br><br>
<form action="combine.cgi?boxes=%number%" method=post>
<small><small>
Enter one word or phrase per line
</small></small>
<br>
%textboxes%
</td>
<td valign="top" width="35%">
<br><br><br>
<small>%checkboxes%</small>
<br><br>
<input type=submit value="%submit%">
<br>
</td>
</tr>
</table>
<table align="center" width="90%">
<tr align="left" valign=middle">
<td>
<tt>
%output%
</tt>
</td>
</tr>
</table>
</html>'
}
# menu function takes calling page and menu array
# as input and returns an array of html which needs
# to be joined with a separator...calling example:
# join " | \n", get_menu($query, @menu);
sub get_menu {
my ($source, @menu) = @_;
my @return_array;
for my $item (@menu) {
my ($title, $destination) = split /::/, $item;
$title eq $source ?
push @return_array, "<b>$source</b>" :
push @return_array, qq{$title}
} ;
return @return_array;
}
# textareas function takes user chosen mode as input
# returns an array of html which needs to be joined
# with a separator...calling example:
# join "<br>\n", get_textareas($query);
#
# also note returns %user_input_0% %user_input_1% etc
# which needs to be swaped out later
sub get_textareas {
my $choice = shift;
my @textareas;
for my $num (0..$#{ $modes{$choice}{textareas}}) {
$textareas[$num] = qq(<textarea name="$modes{$choice}{textareas}[$num]" rows=9 cols=33>);
$textareas[$num] .= qq(%user_input_$num%</textarea> \n);
}
return @textareas;
}
sub get_checkboxes {
my ($choice) = @_;
my @checkboxes;
for my $num (0..$#{ $modes{$choice}{options}}) {
(my $label = $modes{$choice}{options}[$num]) =~ s/_/ /g;
$checkboxes[$num] = qq(<input type="checkbox" );
$checkboxes[$num] .= qq(name="$modes{$choice}{options}[$num]" );
$checkboxes[$num] .= qq(value="$modes{$choice}{options}[$num]" );
$checkboxes[$num] .= qq(%checked%>);
$checkboxes[$num] .= qq(\u$label\n);
}
return @checkboxes;
}
sub check_checkboxes {
}
- Next message: Brian McCauley: "Re: any pointers please? combine words script"
- Previous message: Randal L. Schwartz: "Re: creating shell scripts using #!/usr/local/env perl"
- In reply to: Uri Guttman: "Re: any pointers please? combine words script"
- Next in thread: Glenn Jackman: "Re: any pointers please? combine words script"
- Reply: Glenn Jackman: "Re: any pointers please? combine words script"
- Reply: Brian McCauley: "Re: any pointers please? combine words script"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|