Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am in my learning stages of perl and I am creating a website that implements my progress. This is a script that obviously prints my quote of the day. My question is really a request for comments on my approach. Am I doing anything extra that could be left out? What can I do to improve this? I played around with rand() a bunch of ways until I found out I could use rand the way I did. I know this is a simple script, but I think starting out small is good so I can move what I learn to the bigger scripts :)

Methods I used prior to final choice:
srand; $index = rand(0..10); print "$quotes[$index]"; ---------- srand; @rquotes = rand(@quotes); print "$rquotes[3]";
.... and some others you can see I went through some testing and retesting of ideas until I got one that worked, so I just want to know if my methods are good for learning perl better. Basically I would just like any comments, tips, or criticisms from all. :)
#!/usr/bin/perl -w use strict; srand; my $quotes_file = '../quotes/quotes.txt'; open (QFILE, "<$quotes_file") || die "Error: could not open $quotes_file:$!\n"; my @quotes = <QFILE>; my $quote = $quotes[rand(@quotes)]; print "Content-Type: text/html\n\n"; print "$quote";

Replies are listed 'Best First'.
Re: quote of the day
by wog (Curate) on Jul 07, 2001 at 00:40 UTC
    See the FAQ How do I select a random line from a file?. That algorithm is far more memory efficient then what you have. That said, usually the advice given is to use the CGI module. For a script this simple it probably isn't necessary, but it's a very good idea when you start wanting to parse parameters to your script. Also, when you start doing real CGI programming, you should use the -T flag. I wonder if you really want text/plain for your content type, and not text/html.

    That said, when you had trouble with rand, what I think you should have done, is check out the documentation. There is plenty of documentation on this site, as well as elsewhere. Additionally, documentation comes with perl, in the form of the perldoc command, or HTML files with the (windows) ActiveState version. From the documentation it is fairly easy to rule out the methods you tried that failed. Don't guess on how something works; you can look it up.

Re: quote of the day
by sierrathedog04 (Hermit) on Jul 07, 2001 at 05:55 UTC
    First of all, Anonymous Monk, thanks for the beautifully written script. I modified it somewhat and use it to display a random computer quote on my website. Click here to see it in a new window.

    I have only two comments:

    1. Use CGI.pm to write your HTML headers at least. In scripts which use forms there are additional benefits to using CGI.pm, but even here CGI.pm saves you the trouble of remembering the syntax for the http header.
    2. The use of srand is optional in newer versions of Perl, such as 5.6.
    If you want to see my modified script in all its beauty here it is:
    #!/usr/bin/perl -w use strict; # srand; srand is not needed when using modern versions of Perl such a +s 5.6 use CGI qw/:standard/; # CGI is a standard perl module that helps writ +e Perl CGI scripts. http://search.cpan.org/doc/LDS/CGI.pm-3.03_01/CGI +.pm my $quotes_file = 'quotes_file'; open (QFILE, "<$quotes_file") || die "Error: could not open $quotes_file:$!\n"; my @quotes = <QFILE>; my $quote = $quotes[rand(@quotes)]; # Now use CGI.pm functions to write your HTML print header, start_html('SIERRATHEDOG04\'S COMPUTER QUOTE OF THE DAY'), h1($quote), end_html();
Re: quote of the day
by lhoward (Vicar) on Jul 07, 2001 at 00:37 UTC
Re: quote of the day
by stuffy (Monk) on Jul 07, 2001 at 06:54 UTC
    I just have to do this, sorry Merlyn

    <merlyn>
    I wrote an article about that. {grin}
    </merlyn>
    actually this is a pretty good script, as a newbie, you can learn alot from it. You should check out his full collection of articles and play with his scripts.

    Stuffy
    That's my story, and I'm sticking to it, unless I'm wrong in which case I will probably change it ;~)