Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

CGI Random Quote Server

by ailie (Friar)
on Dec 02, 2000 at 09:45 UTC ( #44556=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info Ellen Knowlton Wilson (ailie)

This is a small script (one of my first) that will display a random quote from a file on a webpage. It allows the use of HTML tags in the quotes.

I'd be interested in any constructive criticism people have to offer.

Mea culpa - I didn't use CGI. I must have had a brain bubble or somesuch when I posted this. Sigh...

#!/usr/bin/perl -Tw 
use strict;

my $data = "/path/to/quotes/here";
my $output;

$/ = "\n";
$data = '/path/to/quotes/here';
open DATA, $data or die "can't open data file: $!\n";
rand($.) < 1 && ($output = $_) while <DATA>;
print "Content-type: text/html\n\n\n";
print "<HTML><HEAD><TITLE>this is the random quote server</TITLE></HEA
print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#0047D5\" ALI
+NK=\"#005500\" VLINk=\"#000080\">\n";
print "<FONT FACE=\"Arial, Helvetica, Sans Serif\"><BR><BR>\n";
Replies are listed 'Best First'.
Re: CGI Random Quote Server
by quidity (Pilgrim) on Dec 02, 2000 at 23:08 UTC

    Just being picky here but...

    use CGI;

    But you didn't.

    $/ = "\n";

    All very well, but $/ will be this anyway, most people use "\n%%\n" as their cookie file delimiter so they can format them to 76 chars/line or whatever so as to fit them in emails as well.

    print "Content-type: text/html\n\n\n";

    But you're printing out a badly formed html document unless each and every cookie starts <html> etc...

    A better solution would be:

    #!/usr/bin/perl -w use strict; use CGI; my $output; my $data = '/path/to/cookies.txt'; my $query = CGI::new(); open COOKIES, "<$data" or die "Cannot open cookie file: $!"; $/ = "\n%%\n"; rand($.) < 1 && ($output = $_) while <COOKIES>; # for plain text: print $query->header('text/plain'); print $output; # or for html print $query->header('text/html'); print $query->start_html('Cookies'); print $output; print $query->end_html;
Re: CGI Random Quote Server
by AgentM (Curate) on Dec 02, 2000 at 10:05 UTC
    This is no biggie, but you might try CGI::Carp since a die in the script now will not pass on to the web client. Also, you're not really using CGI calls at all. Throw in a print header; instead of the current header just for good measure :o). Also, there's no need to interpolate in the last line. For more random quote enjoyment, check out Bone::Easy.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://44556]
NodeReaper wonders through a maze of twisty little sayings, all alive

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2017-06-24 02:25 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (556 votes). Check out past polls.