Updated: 2010-04-30 11:49:28

The web comic xkcd once again points out the importance of Perl:
Updated: 2010-04-30 11:49:28
At the end of each class, we always ask our students to fill out an evaluation form. There are two reasons for this: to find areas where we need to improve, and with the hope that they’ll put down some kind words that we can quote on the site in the testimonials page. [...]
Updated: 2010-04-30 11:49:28
Say that you have a file that looks something like this:
2008-01-02: first entry
2008-02-03: second entry on two lines
here is the additional line
2008-03-04: third entry
has
three
extra lines
2008-04-05: fourth entry has just one on line again
If you need to search for all entries that have [...]
Updated: 2010-04-30 11:49:28
Say you have an array of names: @names=qw(Tom Dick Harry);
If you wanted to sort these, you could just use a simple sort() command: @sorted=sort(@names); That uses alphabetical order for sorting by default. The sort criteria is not given, but you could get the same results by giving a longer version of the sort [...]
Updated: 2010-04-30 11:49:28
How do I find the biggest files under a directory? There are many ways to do this, but it isn’t always as easy as it sounds.
First of all if the directory has no sub-directories, it’s easy. Just list the files sorted by size, which any operating system can do. But if there [...]
Updated: 2010-04-30 11:49:28
In an earlier entry (was it really six years ago?) I talked about the usage of $/ and the -0 command-line option to Perl to change the input delimiter. But there’s another way to read in “slurp” mode that isn’t described there, the File::Slurp Perl module.
File::Slurp provides a function read_file, which given a filename, [...]
Updated: 2010-04-30 11:49:28
As an interpreted language, Perl scripts can generally be run unmodified on any platform. But there are situations where the differences between platforms make it necessary to test what platform you are running on and act accordingly.
Say, for example, that you need to change permissions on a file. On Unix and related operating [...]
Updated: 2010-04-30 11:49:28
One of regular expressions’ most useful features is memorization. To do this, just put parentheses around part of your expression and the result will be memorized:
my($name) = /hello, (\w+)/
In this example, we look in $_ for the word “hello” followed by a comma, space, and a word. Since the word, \w+, has parentheses [...]
Updated: 2010-04-30 11:49:28
Since we’re starting a new year, let’s look at handling dates in Perl. Let’s say the user enters a date and you want to check if it’s between a particular range of start/end dates.
In particular, let’s say you want to go to Hawaii and your kids are in school for the spring semester from [...]
Updated: 2010-04-30 11:49:28
Here’s an easy way to find the largest file in a directory.
First, open the directory to read the list of file names in it.
opendir DIR, $directory
or die "Error reading $directory: $!\n";
Then, read the file names and sort according to size.
my @sorted =
[...]
Updated: 2010-04-30 11:46:59
I often write HTML pages or documentation that includes code samples. When the code is presented this way, it's much easier to follow if it features syntax highlighting. I had found a script that could highlight Perl code, and then I realized I needed the same thing for C code as well. I've posted a new script on my web site that inserts HTML markup into source code files to provide colored syntax highlighting.