Directory Globbing in Perl

Today one of our developers started leaning Perl and was having some problems with an example she’d found on a web site. She called me over to look at her code and I noticed two things right away…

Firstly, the example she was copying from was missing the rather crucial inclusion of the Perl module the example was demonstrating… has fail!

Secondly, I noticed she’d gone straight to the abstracted module rather than using ‘pure’ Perl to open the directory, loop through the files checking them against her own regex and then closing it again. I guess in many ways this is the better way of working - it’s certainly less code and there is less to go wrong… I just hope the module is supported on IRIX.

Here’s the code for any one interested:


#!/usr/bin/perl

use Data::Dumper;
use File::Glob ':glob';

my $path = '/Users/dk0/Desktop/';#your path here!
my @files = <$path*.{pdf,xml}>;

print Dumper(@files);

foreach (@files) {
	print $_ . “\n”;

}

Intel Macs and Perl

I’ve recently migrated my development environment to a MacBook Pro from an iMac G5. I used the old FireWire migration tool, as I’ve never had any problems in the past as it copies everything you could ever need across to your new machine…

… everything including all your Perl libraries which would normally be great, but when your migrating to a machine with a different architecture from your old one (ie. PowerPC to Intel) then this is going to cause problems!

The solution? Dust off and nuke from orbit - it’s the only way to be sure!

Only in this case you can use a pocket nuke and simply rename darwin-thread-multi-2level folder in the /Library/Perl/5.8.6 and make an empty replacement for it… This will replace all the obscure errors about ‘dynamic library loaders’ with a good old ‘you dont have it installed’ - Which is something cpan can help you with!

There’s probably a better solution to this problem, but this simple fix got me out of trouble today!

Perl’s MIME::Base64 on Mac OS X 10.4.9

I was having a problem using MIME::Base64 on Mac OS X. My script would run on the RedHat Dev server but not on my Mac Desktop which was making developing a bit of a nightmare.

The error was:


MIME::Base64 object version 3.05 does
not match bootstrap parameter 3.07 at
/System/Library/Perl/5.8.6/darwin-thread-multi-2level/
XSLoader.pm line 92.
Compilation failed in require at
/System/Library/Perl/5.8.6/darwin-thread-multi-2level/
MIME/QuotedPrint.pm line 14.

The weird thing was that MIME::Base64 was already installed and up to date! After a few brief minutes of frustration I went into CPAN and gave it a:


force install MIME::Base64

And lo and behold… it work! I guess when it said it was already installed it didn’t check dependancies or something. I wonder if there are any pre-installed Perl modules in OS X that aren’t quite installed… hrmmm.

Perl: Syntax error with eval in strict mode

I spent twenty minutes this morning trying to fix a syntax error in a Perl script. It turned out that, despite three independent examples I found via Google and my copy of Pro Perl, I’d left off a semicolon after the end curly bracket of an eval{} block!

I mention it here to remind my self that one person can never know everything - especially about Perl! I heard some one say that Perl is a dead language, it’s not - just a difficult one!

The correct syntax for a strict eval{} block:


eval {
      die('my error message!');
};

if ($@) {
      print 'Oh Dear: ' . $@ . "!\\n";
}

#the above will print: Oh Dear: my error message!

DBI: Possibly better than your Mum.

DBI
I’m writing a Perl script that’s using more CSV text files than I care to think about as a data source… I spent half a day wasting my time with Text::CSV which was struggling with carriage returns inside text fields (which I’d argue shouldn’t be there any way) and all the other usual annoyances you find working with CSV files…

But then like a shining knight in armer DBI::CSV came and rescued me. Imagine being able to say “Here’s my data source, by the way it’s a CSV file…” and then being able to query that data source with SQL! Need to add up a load of numbers? Don’t loop through the lines in the text file with “+=”… just use SUM()! Need to narrow done the results set? That’s what ‘WHERE’ is for…

Read this and join me in the ‘l33t dance of pr0ness.

Perl to check Drive Space

Well the server filled up again and as lead developer on he project it was left to me to sort it… so I wrote this Perl script to run under cron to send me an alert when the disk usage goes about 90%.

To use it just run the script with the path to the mount point of the drive you want to check:
./myScript.pl myMount

Here’s the code (obviously replace the prints with however you want to be notified):


#!/usr/bin/perl

use strict;

my $mountPoint = shift;

my $cmd = "df -h | grep " . $mountPoint;
my $result = `$cmd`;
my @lines = split("\\n", $result);

foreach(@lines) {
	if ($_  =~ m/([0-9]{1,3})%/) {
		if ($1 > 90) {
			print "it's getting full in here!\\n";
		} else {
			print "it's all good!\\n";
		}
		exit;
	}
}

print "Couldn't find mount point!\\n";

In other news… that project I was working on still isn’t finished.