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…)
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…)
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…)
Here’s an easy way to find the largest file in a directory.
(more…)
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…)
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 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…)
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…)
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…)
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…)
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