Perl 101: Things Every Perl Programmer Should Know.

IT'S A TRAP!

while(<STDIN>)

Be very careful of this! If somehow, you were to get a false value (e.g. an empty line, or a line with only a zero on it), your file would stop processing. This doesn't normally happen if you're dealing with file reading (unless you've modified $/), but it's possible.

You're more likely to run into this:

    while(readdir(DIR)) {

If you have a file name of 0, your program will stop and not continue processing files.

A more appropriate while loop looks like this:

    while ( defined( my $line = <STDIN> ) ) {
    while ( defined( my $file = readdir(DIR) ) ) {

arrays are not hashes: $a[time()]++;

If you are using arrays with large numbers, Perl will create an array of that size automatically. Thus, if you're trying to make a count of things that happened during a particular second, avoid the temptation of using "time" in perlfunc as an array key.

Perl does not support sparse arrays. The closest thing is a hash.

DateTime::TimeZone (!)

If you're like me, you either installed the DateTime module for a specific need, or you had it installed as part of your OS.

Did you know there's a seperate dependency, called DateTime::TimeZone, that is a dump of time zone data?

If DateTime can't figure out your time zone environment, it uses DateTime::TimeZone, which can be years out of date!

Update your DateTime::TimeZone today!

Time::HiRes might not be

Don't depend on Time::HiRes for millisecond (or smaller) calculations.

Some systems have a clock_getres that can only go to hundredths of a second, even in this day and age - a recent installation of Red Hat Enterprise Linux 4 on an AMD 64 platform provided only that level of granularity.

So, check your target's Time::HiRes before relying on it.


Want to contribute?

Submit a PR to github.com/petdance/perl101


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.