I recognize that this may be too much to ask of total strangers on a forum, but I am a complete and utter self-taught Perl newbie (have been crash-coursing with online tutorials over the past two weeks) and I’m struggling to write a script for a self-designed project that’s due in two weeks. I have no formal programming education, just self-taught HTML and CSS.

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).

I’m envisioning the randomizing element working like this:

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:

#!/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"; } }
So will this work, assuming I get all my syntax correct and handle the templating issue properly?

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


In reply to will this random-text script work? how do i integrate a template? by hawthorne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.