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…)
Do 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 arean";
}
(more…)
Perl’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.
(more…)
If your program uses certain values which are defined as global variables at the top of the program, and which are kept constant throughout the program (Example: a filename, hostname, URL, user ID, or some other static configuration value), consider using Perl’s "use constant" mechanism to define that value instead. Here’s what it looks like:
use constant data_file => "/home/bill/data.txt";
use constant pi => 3.14159;
(more…)
Perl offers a variety of ways to convert numbers from one form to another (remember, "There’s More Than One Way To Do It").
(more…)
When programming CGI scripts for a web-based application, one common problem is that if there is an error all you see is "Internal Server Error" and you have to go look in the server’s log file for details. But sometimes you don’t have access to the log file, such as when it is hosted on an ISP’s server. Here’s a way to get those errors sent to the screen where they can be of use to you.
(more…)
Try the Text::Wrap module if you need to reformat text to fit in an 80 character window, or whatever other size you might need. This is a module that comes with Perl, so you don’t have to worry about installing it or whether it will be installed on a machine you will be running your programs on.
(more…)
Copyright © 1995-2007 William R. Ward dba Bay View
Training. All Rights Reserved. “Bay View Training”,
“Bay View Consulting Services”, “Bay View
Software”, the sailboat logo, and the domain name
“bayview.com” are trademarks and/or service marks of
William R. Ward dba Bay View Training. For more information,
contact
webmaster@bayview.com