Re: any pointers please? combine words script

From: steve_f (me_at_example.com)
Date: 08/03/04


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 {

}



Relevant Pages

  • Cant make this page work
    ... I can't make this script work properly. ... The script at the bottom of the html page ... Does someone have a perl ... sub output_trace_headers { ...
    (comp.lang.javascript)
  • Re: How to pass info to a CGI program without using a form?
    ... The image on the html page is specified by this html code: ... simply doing something like <url of CGI script>?key and check for the value ... and your script will find what key is there: ...
    (perl.beginners)
  • Re: Asp.net 2.0 and Dreamweaver
    ... I have not used the inline approach, i have used the code behind ... I think the part where it talks about the inserting of script ... One of them is HTML view which shows all of the html and ... Sub Page_Load ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Need help
    ... My script works well with the sample data that I ... when the cgi script is run. ... If you are having a problem with your Perl script and want help, ... click in it and paste my html content or script in it. ...
    (comp.lang.perl.misc)
  • Re: CGI - HTTP 500 Internal server error
    ... What is a "HTML error"? ... most common problem is, as stated above, a missing script header. ... number and attach a STRSRVJOB and STRDBG to it to do debugging. ... To make CGI scripts a bit more verbose, have a look at the ScriptLog (or ...
    (comp.sys.ibm.as400.misc)