I feel like I’ve come as far as I can on my own, and now I need to know:
1. If my idea is conceptually sound
and
2. The best way to implement templating for this particular script
I apologize in advance if my Perl knowledge is so rudimentary that I need further explanation of your explanations.
My project is called The Spam Oracle. It’s a simple website with an HTML form; enter a question and hit submit, and a random snippet of spam is returned as the answer to your question. (The front end is already mostly built: here.) I want the user’s submission to have some bearing on what’s returned, but in a randomized way (the project is meant to be working with Burroughs-esque cut-up concepts).
The script uses the second-to-last character of the submission to determine which one of five spam-filled text files it will open (a-e opens infinity01.txt; f-j opens infinity02, etc) and then returns a random number (between 1 and 5) of randomly selected lines from that file. I’ve decided to use the second-to-last character because the final character will frequently be a question mark, and I imagine the first will often be a W (Who, What, When, Where, Why, etc) or H (How) – which will make the randomization appear quite stultified.
Here’s a very rough draft of my script, with some questions in the comments and a couple more at the end:
So will this work, assuming I get all my syntax correct and handle the templating issue properly?#!/usr/bin/perl –Tw use strict; use CGI ':standard'; use Template; my $t = Template->new; my $cut = param(‘cut’); # submit button’s name attribute = cut my $cut = substr($cut, -1, 1); # redefine ‘cut’ to be only 2nd-to-last letter of submission # does this substr overwrite the param definition of $cut? my $range = 5; # define number of lines (for random_line function) as a # random integer between 1 and 5 my $minimum = 1; # with an offset so range starts at 1 and not 0 my $nr_of_lines = int(rand($range)) + $minimum; # analyze $cut value, open appropriate infinity file and # call random number of lines if (my $cut =~ /[a-e]/i) { # does $cut need “my”? if yes, inside or outside parens? open (INFINITY01, "infinity01.txt") or die "file is temporarily unavailable: $!\n"; # need another nested set of curly brackets for print func? # or is print irrelevant; ie, should the template be called # here? where does the flow control language belong – here, # or in template? and how should it be phrased? print random_line($INFINITY01 [, $nr_of_lines]); # is $INFINITY01 the correct way to reference the file here? # should it be $infinity01? infinity01.txt? # why is $nr_of_lines in brackets, and what function does # the comma serve? I found this function at cpan, but # haven’t been able to find much else about it online. # does “my” need to be added to $nr_of_lines? if yes, inside # brackets or out? close INFINITY01; } elsif (my $cut =~ /[f-j]/i) { open (INFINITY02, "infinity02.txt") or die "file is temporarily unavailable: $!\n"; print random_line($INFINITY02 [, $nr_of_lines]); close INFINITY02; } elsif ($cut =~ /[k-o]/i) { open (INFINITY03, "infinity03.txt") or die "file is temporarily unavailable: $!\n"; print random_line($INFINITY03 [, $nr_of_lines]); close INFINITY03; } elsif ($cut =~ /[p-t]/i) { open (INFINITY04, "infinity04.txt") or die "file is temporarily unavailable: $!\n"; print random_line($INFINITY04 [, $nr_of_lines]); close INFINITY04; } elsif ($cut =~ /[u-z/i) { open (INFINITY05, "infinity05.txt") or die "file is temporarily unavailable: $!\n"; print random_line($INFINITY05 [, $nr_of_lines]); close INFINITY05; # include fail-safe statement in case $cut doesn’t match any # letter of the alphabet } else { print "Yes, no, maybe so.\n"; } }
Also: Do the spam source files (infinities) need "\n" characters at the end of each line in order for the rand function to work (i.e., to recognize each line in the file as a separate line)?
I know it’s a huge favor to ask for so much help on such rudimentary code. Thanks a million, in advance, to anyone who can assist with any or all of this.
cheers,
jess
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |