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:
Modifying a File Without Changing Its Timestamp
October 25th, 2004Using Scalar Variables in Regular Expressions
August 4th, 2004If 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!
Regular Expressions to Parse Data Files
March 25th, 2004Regular 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.
Writing Configuration Files in Perl
July 17th, 2003It 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.
Validating Data Types
July 17th, 2003Perl 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.
Anonymous Scalars
March 27th, 2003If 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:
Non-Greedy Regular Expressions
February 12th, 2003Regular expressions in Perl are "greedy." That means that if you use a * or + operator in a regular expression, it grabs as much of the string as it can. This can be frustrating at times, but it’s useful in other respects. Consider this:
my $ip_addr = "192.168.1.2"; my ($network, $host) = ($ip_addr =~ /(.+)\.(.+)/); print "network=$network host=$host\n";
You need a way to know for sure whether $network gets "192.168.1" and $lastpart gets "2" or whether the split is "192" vs. "168.1.2". The decision was made to have it be "greedy" which means that the first + grabs the lion’s share, and the second one gets the leftovers. Put another way, the first one gets as much as possible short of making it impossible to match the string.
Constants Again
February 12th, 2003Do you ever write programs that use constants? You probably do without knowing it. The canonical example is a geometry program that uses the special number π (pi):
my $area = 2 * 3.14159 * $radius;
But what if you have multiple parts of your program where you have to put the value, and maybe you mistype it in one place, or use the wrong number of digits? So you put it in a variable at the top of your program:
my $pi = 3.14159; ... my $area = 2 * $pi * $radius;
That’s fine. But what if you accidentally modify the value of $pi? It’s not so hard as you think. Consider this buggy line of code. The programmer wanted to compare the value of $pi to the formula, but used = instead of == by mistake:
if ($pi = $x / ( 2 * $radius ))
{
print "$x is the area\n";
}
Copying an Array, with Modifications
December 6th, 2002It is often necessary to make changes to the elements of an array while copying it to a new variable. You might be making a regular expression substitution on each element, or only copying elements that match a certain pattern. In this column I’ll show you three different approaches to this problem, in the spirit of Perl’s motto, "There’s More Than One Way To Do It" (TMTOWTDI):
Benchmarking Your Code
September 6th, 2002Perl’s motto is "There’s More Than One Way To Do It" but sometimes you may have trouble determining which is the best way to do it. Here’s one way to pick the best way: the Benchmark module.
The Benchmark module works by running your code many, many times and averaging the amount of time it took to run each one. It then reports on the total amount of time taken.