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

My, oh my! You have far too many my's in your code/

Each my for the same variable will mask the previous contents of that variable so it is effectively lost to your script. So only use my once for each variable.

As for opening and reading your text files, I don't know how large they are, but if they have quite a size, the web-server will take a big hit if for every access to your script it has to read a huge file just to get a few random lines. In such case it is much better to put all your lines in database and get the content from the database.

Finally, where did you get the random_line subroutine? I see it nowhere defined in your script, so I would be surprised if it works.

PS: I wouldn't bother with finding the second to last letter of the input and using that as a randomizing device. Just do a straight random pick out of all of your lines of spam-quotes. It will work as good and none will be the wiser.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re^2: will this random-text script work? how do i integrate a template?
by CountZero (Bishop) on Jan 15, 2007 at 06:49 UTC
    You should start your script with a use File::Random qw/:all/;

    This will load the File::Random module and make all of its subroutines available in your script.

    Then you can do something like:

    my $dir = '//path/to/my/SpamQuoteFilesDirectory'; my $file = random_file(-dir => $dir); my @lines = random_line($fname, int(rand(5)) + 1);
    This script will choose at random 1 file out of all the files in your '//path/to/my/SpamQuoteFilesDirectory' and from this file choose at random 1 to 5 lines and store them in the array @lines.

    BTW, you did install File::Random first didn't you?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Thank you! And one more question -- hopefully the last: I've put File::Random and Want in a directory within the home directory of my hosting account, per related advice given here.

      What's confusing me are these instructions at CPAN, about running the Makefile.Pl, and additionally the reference in the Dreamhost link above about the "use lib" command. I've tried to research all this myself, but I think I don't know enough about programming to figure it out.

      Where/how do I need to take these final steps, so my script knows where to find the modules?

        Okay -- I think I've figured out the  use lib thing.

        Is this right?

        #!/usr/bin/perl –Tw use strict; use CGI ':standard'; use lib qw("/perl_modules"); use File::Random qw/:all/; my $dir = '//spam_oracle/infinity'; my $file = random_file(-dir => $dir); my @lines = random_line($fname, int(rand(5)) + 1);

        Specifically, will the script know to drill down within the perl_modules directory, if the modules are within subdirectories?

        And is that the correct path, if the script itself is in a cgi-bin within a subdomain on my account (mact.jesskilby.com/cgi-bin/oracle.pl)?

Re^2: will this random-text script work? how do i integrate a template?
by hawthorne (Novice) on Jan 15, 2007 at 00:47 UTC
    Yeah, the my thing was really confusing me. Thanks for the clarification.

    How large a text file is too large to be practical, roughly speaking? I know even less about databases than I do about Perl, so I'd like to stick with some semblance of my original approach if it's feasible.

    Last, I found the random_line subroutine here: http://search.cpan.org/~bigj/File-Random-0.17/Random.pm#FUNCTION_random_line(%24filename_%5B%2C_%24nr_of_lines%5D)

    What do I need to add to my script to make it work?

    (And as I continue to struggle with this project, the option of simplifying things down and abandoning the randomizing device becomes more and more attractive.)