Platform-Specific Perl

in Perl Tips, Files & Directories
by William Ward on October 2, 2006 8:29 pm

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. (more…)

Ignorance is Bliss - non-memorizing parentheses

in Perl Tips, Regular Expressions
by William Ward on April 20, 2006 2:07 pm

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 around it, the part of the string that it matches gets memorized. In this example, we are assigning the return value of the regular expression match to $name. So if $_ contains “hello, world” then $name gets “world” - very convenient.

But parentheses also do other things besides memorize their contents, and this feature can become annoying. Here’s an example. (more…)

Dates in Perl: Hawaiian Vacation Planning

in Perl Tips, Miscellaneous
by William Ward on January 5, 2006 5:27 pm

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 January 9 through June 2. Your travel agent gives you a list of possible dates when you can go to Hawaii really cheaply, and you want to know which ones conflict with your kids’ school schedule so you can include the budget for a babysitter in the cost of the trip.
(more…)

Finding the Largest File in a Directory

in Perl Tips, Files & Directories
by William Ward on February 22, 2005 5:31 pm

Here’s an easy way to find the largest file in a directory.

(more…)

Modifying a File Without Changing Its Timestamp

in Perl Tips, Files & Directories
by William Ward on October 25, 2004 3:51 pm

Your file system keeps track of when each file was last modified. But have you ever wanted to edit a file without affecting its timestamp? Using the "utime" function, which is built in to Perl, you can! Here’s how:

(more…)

Using Scalar Variables in Regular Expressions

in Perl Tips, Regular Expressions
by William Ward on August 4, 2004 3:20 pm

If you know how to use regular expressions you will know about special codes like ^, $, and \w which can be used to indicate position or certain classes of characters in the string. But in fact, anything that works in a double-quoted string can be used in a regular expression!

(more…)

Regular Expressions to Parse Data Files

in Perl Tips, Regular Expressions
by William Ward on March 25, 2004 3:52 pm

Regular expressions are the best way to parse text in Perl. And when combined with the hash data structure, you can easily build an in-memory structure based on data read in from a file.

(more…)

Writing Configuration Files in Perl

in Perl Tips, Files & Directories
by William Ward on July 17, 2003 1:00 am

It is often useful to have a configuration file for a program, where you can specify certain variables that are used in the program. Examples of configuration parameters might include files, email addresses, usernames, or passwords the program uses, etc. If your Perl program needs to read a configuration file, there are lots of ways to do it.

(more…)

Validating Data Types

in Perl Tips, Regular Expressions
by William Ward on July 17, 2003 1:00 am

Perl is not a strongly-typed language. A scalar can hold a string or a number or a reference. But sometimes you need to know what it contains, for example if you are communicating with a strongly-typed system like a relational database, or even if you just want to make sure the user entered a number for his age rather than "old enough" or something of that kind.

(more…)

Anonymous Scalars

in Perl Tips, Data Structures
by William Ward on March 27, 2003 3:38 pm

If you’ve done much Perl programming you’ve probably encountered the Anonymous Hash, Anonymous Array, and probably Anonymous Subroutines as well. Did you know there’s also an Anonymous Scalar? It’s one of those things that is not very useful most of the time, but is the perfect tool for some situations. Here are two situations where you can use them, however:

(more…)


Next Page »