Tkil's Perl Examples

Contents

Commentary

Tkil's Suggestions for the #perl IRC channel
I wrote up a quick summary of what I feel is acceptable behavior on the EFNet IRC #perl channel. Give it a read, and please feel free to send me comments!

Another regular on #perl, oznoid, has written up a shorter list of his take on #perl rules. We mostly agree with each other; if you want a different take on the channel rules, please check his out. freeside, another regular on #perl, has also sketched out some rules-of-conduct for #perl. Again, not too far distant from my own views. I'll additionally add that freeside is one of the absolute saints on #perl.

If the last paragraph meant nothing to you, either ignore it, or check out the glories of Internet Relay Crack (and watch your free time go *poof*!)
[Tuesday, 1999-03-02]

Update: RKMR has kindly informed me that many of the regulars from the old EFNet #perl channel are now meeting on a new server: irc.infobot.org. I suspect that there is still a #perl channel on EFNet; however, when I wrote this document, I felt that it reflected the views of a majority of the regulars on that channel. As such, it is probably more applicable to the "new" #perl channel than to whatever has sprung up on EFNet. Either way, I still feel that these are good ideas for any technical channel, and I hope they continue to be useful.
[Wednesday, 2001-02-14]

What language should I learn?
A friend asked me, on behalf of her brother, what language(s) I would recommend a beginner learn. My answer follows; if you have any comments to make, feel free to e-mail me, and i'll see if I can't add them to this page. Thanks!
[Wednesday, 1999-04-21]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

Text Manipulation

escape-commas (numbered)
This program has its roots in a request made by goss (now known as bekj) on #perl a long time ago; December 1997 was the original date. She had a problem where she wanted to "escape" commas (by prefixing them with a backslash), but only if they were inside brackets. To make the situation hairier, the brackets themselves could be escaped.

My favorite bit of this program is the use of parentheses in a split statement, to break out parts of the string to treat differently.
[Saturday, 1999-05-29]

jumbles (numbered)
Solves "Jumble" style word games. Algorithm suggested by Fmh on EFNet's #perl channel, in response to question from su-
[Sunday, 1998-12-13]

png-text (numbered)
A quick script to extract "tEXt" chunks from PNG files. Interesting primarily because it works with binary data in a straightforward fashion.
[Wednesday, 1999-01-27]

rev-sort (numbered) (and a slightly different version: rev-sort-2 (numbered))
Inspired by a question from wawb, this illustrates a fairly hairy sort routine designed to put "revisions" (such as "1.3" or "4.5.12") into ascending order. It seems to me that I could make it smaller, but this is what came to mind first -- and it does seem to work.
[Monday, 1999-04-19]

rot13-dict (numbered)
We got to talking about rot13 encoding (just a simple transposition cipher), and I was curious what words would rot13 to other valid words. Most amusingly, the word "tang" rot13's to "gnat" -- which is also it's reverse!
[Tuesday, 1999-03-02]

simple-template (numbered)
One of the most frequently asked questions on #perl involves expanding variable names in text strings, to do a sort of template or mail merge. Using actual variables for this is a bad idea, since you either have to duplicate perl's internal functionality to expand them, or you run the risk of expanding the wrong things. Using your own templating system gets around most of these issues; this is a simple example of such a system.
[Sunday, 1999-04-18]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

Systems Administration

archive-ml (numbered)
I'm subscribed to many mailing lists. My preferred way to read them is to use procmail to split the incoming mail into various spool files, then split those spool files into individual messages using the nnml select method inside of Gnus (the insanely powerful newsreader that, in turn, runs inside the insanely powerful XEmacs and Emacs editors).

Unfortunately, this technique piles literally thousands of files in single directories, which makes many operations on those directories painfully slow. This is my answer to that problem; it goes through an entire directory, and tries to put individual files into subdirectories based on year and month.
[Sunday, 1999-06-13]

df-hash (numbered)
A simple example of how to build a hash-of-hashes dynamically. As flyte pointed out to me on IRC, however, this is only dynamic at the first level; the second level ("inner") hash is static. Oh well. It's unix-based, but the concept is pretty simple (assuming, of course, that you know what the "df" command does on unix...)
[Wednesday, 1998-11-11]

logwatcher (numbered)
A gaudy little app that might be useful on the head of a router somewhere. It displays current stats and a "tail -f" output of the system log. So far, I've only tested it on my Linux 2.2 box. It requires the Curses module from CPAN. Inspired by the firewall statistics program from Tummy.com.
[Tuesday, 1999-10-12]

make-user-dirs (numbered)
How to go through your /etc/passwd file and create a bunch of directories. Dedicated to MorDecai. :)
[Thursday, 1998-12-10]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

Network Programming

Also known as "Client/Server" programming, mostly using the TCP/IP protocol stack.

io-socket (numbered)
A trivial example demonstrating usage of the IO::Socket class. Someone on IRC asked, "Can I read and write from the same socket in perl?" The answer is yes (obviously), but a concrete example never hurt.
[Saturday, 1998-11-07]

martin-server (numbered) and martin-client (numbered)
Another version of the client and server scripts offered in the perlipc documentation, these two programs show some trivial interactions between a client program and a server. Nothing too impressive, but it is a nice, short example that covers the basics. Written in about an hour, in response to an e-mailed question.
[Wednesday, 2001-02-07]

perl-chat (numbered)
A very simple "chat server", modelled loosely after IRC interactions, but not nearly that complex. Mostly an exercise to convince myself that I really did understand the four-arg select function. This is not very secure, nor is it very flexible, but it was pretty darned amusing.
[Saturday, 1998-05-15]

port-forward (numbered)
A slightly more advanced program that uses IO::Select to redirect ports. All the values here are hard-coded, but the implementation is straightfoward. In response to a question by dynweb on EFNet #perl
[Saturday, 1999-05-22]

reverse-lookup (numbered)
A simple demonstration on how to look up hostnames given IP addresses.
[Monday, 1998-12-14]

tkil-masks (numbered)
veblen had a neat idea for dumping a table of netmasks and associated values. He asked me for a critique and/or a compression, and this was my answer.
[Sunday, 1999-07-11]

udc-server (numbered) and udc-client (numbered)
These are expanded versions of the examples given in the perlipc man page for doing UNIX-domain sockets. The primary advantage in these versions (over the original) is that the server code includes the support routines which are spread out in the original documentation.
[Monday, 1999-02-08]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

WWW

fully-qualify (numbered)
Uses HTML::Parser and URI::URL to present an HTML document with all of its links "absolutized" to the appropriate base URL (either the original document URL, or the contents of the <BASE ...> HTML element. Inspired by a question from "Tac/Smokescreen Action Network" <tac@smokescreen.org> on the libwww-perl mailing list.
[Thursday, 2000-01-20]

get-link-desc (numbered)
Uses HTML::Parser to get a series of URLs along with any text inside the link. Inspired by some questions from phr.
[Monday, 1999-08-02]

link-extract (numbered)
A simple example using HTML::LinkExtor to yank some links out of a URL.
[Wednesday, 1998-12-09]

mp3-find (numbered)
Go through a directory hierarchy, looking for MP3 files. Output an HTML page that has convenient links to all of them. One of the more amusing points of this program was realizing that since I was splitting paths on "/"s, I could use "/" to start "private" keys in a hash that dealt with file names.
[Friday, 1998-11-27]

mp3-find-freeside (numbered)
My IRC friend Freeside came up with this basic algorithm as a one-liner. I reformatted the one-liner to be readable. It still illustrates Freeside's point -- sometimes, less is more.
[Friday, 1998-11-27]

redirect-check (numbered)
Follows a list of possible redirections, using a nifty feature of HTTP::Response.
[Monday, 1999-07-19]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

CGI

browse-cds (along with its browse-cds-devel development version and MySQL DDL) [as CGI]
My most ambitious CGI to date. This has been my big "teach myself CGI and DBI programming" project. It uses MySQL to present a dynamic listing of my CD collection. Nothing too earth-shattering, I know, I know. But I did learn a fair bit. And yes, it's ugly as sin. Sorry.
[Tuesday, 1999-06-08]

checkpass (numbered) [as CGI]
A very simple script which checks for cleartext passwords. It's not very secure, and it doesn't handle the important concept of sessions at all. That is, once you've verified that they are who they say they are, how do you keep that information across different page visits? Inspired by a question from adidas.
[Thursday, 1999-06-10]

just-cds (numbered) [as CGI]
A simple fore-runner to the browse-cds CGI. It just dumps the entire contents of my Album table.
[Wednesday, 1999-06-02]

line-number (numbered) [as CGI]
A quick and dirty little utliity for displaying text files (typically, code listsings) with line number and green bars. I believe Randal Schwartz did something similar in one of his columns, but I can't seem to find it.
[Sunday, 1999-12-19]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

Utilities

bit-count (numbered)
Various ways of counting the number of bits set in a 32-bit integer, along with timing data.
[Wednesday, 1999-06-02]

iso-timestamp (numbered)
Use localtime and sprintf to make a decent timestamp in the ISO Fashion. In the meantime, I've come to conclude that using POSIX's strftime is almost always sufficient to my needs.
[Sunday, 1999-01-31]

map2 (numbered)
A two-list version of the built-in map function. Illustrates a useful use of subroutine prototypes.
[Sunday, 1999-05-30]

switch-example (numbered)
A brief example of how to implement a "switch" statement inside perl. A much more exhaustive treatise is available from Tom Christiansen.
[Thursday, 1999-04-15]

Weekday.pm (numbered) (and an test script (numbered))
Determine day-of-week from date. Algorithm from CRC Math Handbook (30th Ed).
[Sunday, 1998-12-13]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index

Math

point-in-hull (numbered)
pdt posed a bit of a teaser the other evening on #perl. Given a set S of points (which, nominally, are the boundary of a possibly non-convex or self-intersecting polygon, but that doesn't matter here), and a point P, determine whether P is in the convex hull of the points in S. (All in 2D, in case it wasn't obvious.)

The solution had to be of order n, the size of the point set S. My solution is probably at least a little wasteful, but it does seem to work.

[Wednesday, 1999-12-01]

Top [ Commentary | Text Manip | Admin | Networking | WWW | CGI | Utilities | Math ] Index


Index


Tkil
Last modified: Wed Feb 14 19:18:07 MST 2001