Apr 1
By The Grace Of Perl – Part 2
So I wanted to make at least one more post about some seriously powerful features of Perl that usually go unnoticed. Since most people who write Perl code to look like the crummy C/C++ code they normally write, I’m hoping at least one person will read this and start doubting the way they do things in Perl.
Tie. Not the sort of concept that needs a breaker, not the style of food or country of origin, but more like something that needs an interface. The tie keyword is probably my newest, yet favorite, feature of the Perl language. Basically, it allows a class to provide an interface, using specifically named subroutines, to access any form of data. The typical tie syntax requires a variable, a class name, and scalar. The class name provided is the class that will implement the required interface for the type of variable provided in the first parameter, and the last parameter is an optional parameter to the class’s constructor. To provide an illustrative point, let me show you a gorgeous example from Tie::File:
tie @array, 'Tie::File', $filename or die "Error loading $filename: $!\n";
At this point, the Tie::File module will provide the necessary interface to access any arbitrary file by random access to the array provided, @array. That it you can modify any arbitrary line in a similar fashion:
$array[13] = 'blah'; # line 13 of the file is now 'blah'
Now remember, since this technically ties the file to the array, all standard list context funtions apply, such as push, pop, shift, splice, and others. It should be noted that Tie::File is a packaged module (packaged with Perl, that is).
I marvel in the excellence that is tie and Tie::File. The mind reels thinking of the possibilities. You could sort an entire file’s contents, modify the file in place, and write it back out in a matter of a few (at most) lines of code! I can’t believe I used to do things any other way! Of coarse this module has its drawbacks, most notably is this could be a huge drain on very large files.
So many things have tie interfaces, it’s any wonder that most Perl programmers still do things the hard way. Go take a look for yourself, you might find a ton of modules that you never thought existed that could save you countless hours writing and debugging your own version of the same thing.
The last blurb I want to mention is about references. There are few other higher-order languages that are able to abstract memory management away from the developer, yet provide the capability of referring to something by its reference. C# has a crappy IntPtr, which is not only difficult to use but is also platform-specific. In practice, it’s use is akin to writing Java JNI code (large JNI interfaces can be Hell On Earth).
You can refer to all object by their references. Clever usage of context can result in serious performance improvements. Passing large arrays or hashes to a subroutine directly is akin to passing large C++ object by value.
I use references whenever I want to abstract out the type of the data or if I aim to improve the performance of something. I spent extra time switching to all reference usage in my 9 Man’s Morris project for AI and it was very easy to switch their context to the true type.
For more on how references work, refer to the perldoc page. I did find another great blog posting where someone detailed the joys of the more esoteric functions of perl. I do highly recommend reading it as it will make your coding more pleasurable.
1 comment
1 Comment so far
Leave a comment
Article of the Day: By The Grace of Perl
A friend of mine just posted a couple of great blog articles about the things he has learned, and the things he loves, about the perl scripting language:
By The Grace Of Perl
…First, the map function is the best example of simplicity. Here is an…