NB: These pages were mostly written in 2001 or so. The résumé dates are accurate but the code is aged and unlike whiskey, 8 year-old code doesn't usually taste better. For a look at my current skills and to see my CPAN modules, sample code, and code discussions, please see these pages instead: Perl resources and sample code and PangyreSoft.
Random quote CGI
Social links
View Ashley Pond V's profile on LinkedIn
Miscellaneous

Other pages

Description

There is a famous Perl one-liner for selecting a (mostly) random line from a file. This CGI is a simple application of it which also relies on the virtual file of the __DATA__ (or the __END__) section of the CGI itself. This keeps it all in a simple unit and saves you from having to read an external file or data source.

While fairly simplistic, this approach is efficient and if you are manually managing a set of quotes (ie, not doing 10,000 of them from a DB or something), this is a good way to go.

Sample quote (reload to pick again)

Code

Description

Once it’s set up, it’s possible (if the server has SSIs on) to call it like so in the HTML of any given page:
<!--#include virtual="/perl/code/random-quote.cgi"-->

If you’re running IIS or something else that doesn’t do server side includes or won’t do ones that execute code you could turn instead to iframes. (Thanks to Peenie Wallie for reminding me about this.) They aren’t directly supported in the XHTML 1.1 specification but they are so useful to advertisers and webservices that they seem unlikely to go away.

Random quote from iframe

Layout becomes somewhat more difficult with iframes. They act like mini-pages so you have to pick fixed (or % based) heights and widths and hope your content will match up correctly. It’s possible to use JavaScript to resize it once it has content but it’s a bit complex and I’m not covering it here.

The only trick in the code, besides the one-liner, is setting your record separator to 2 newlines “\n\n” so that you can have multi-line quotes. It also means you have to ensure the blank lines don’t have any spaces in them, like “\n \n.” That would slurp up the two quotes around it. Also note, different systems might use a “\r\n” for the newline and not just a “\n,” so you’d need “\r\n\r\n” or “\r?\n\r?\n” which works if they are there or not but isn’t the best if you know what line endings you’ve got on the system/computer hosting the webserver.

Search these pages via Google
Text, original code, fonts, and graphics ©1990-2009 Ashley Pond V.