Java Site MenuProgramming SectionsMiscellaneous StuffConsultancy ServicesDownloadsFeedback Form


Perl Modules [Home]


Perl scripts can get very complicated, and even splitting code into subs may not be enough to clarify the situation. Usefully, Perl scripts can be split into modules containing procedures and functions, or if required, fully fledged objects with methods and variables. This capability is extremely widely used in the Perl distribution whose programmers use it to create encapsulated logic with simple interfaces that can, if needed, be extended, overidden or dynamically loaded.

Fully object oriented classes can be created and instantiated so that logic and state can be kept apart, enabling multiple objects of the same class to be safely used knowing that the instance variables for each are totally separate. There are plenty of tutorials around that deal with the creation of modules and objects, the use of packages to gather classes into logical and functional areas and the different ways of representing the internal structure of an object. Look at the Perl documentation at perl.com for a good start point.

Here are a few handy examples.


Persister.pm

It is often useful to be able to see the structure of a complex Perl variable that is made up of hashes and arrays and so on. This is a handy static module to save, or persist, the contents and structure of a variable to a file, or to STDOUT.

The output is indented according to the structure, with each sub-level of hash or array being an extra tab deeper than the last, as illustrated below.

$variable = {
	'akey' => "some string",
	'array' => [
		"another string",
		"stuff",
	],
};

Circular references are dealt with by outputting a placeholder string in-situ, and then replacing this with the appropriate referred value (reference to part of the same structure) at the end of the output file using a simple assignment statement. This works well with eval as expected.

The eval problem

You may already know that there are a number of modules available from CPAN that can do something similar to this one, but I have had a great deal of trouble with some, especially with very circular or very large and deep structures. By very large I am talking about tens of megabytes in size when decomposed into a text file.

The trouble arises when trying to recompose the variable in a subsequent script, generally using the eval method. This can take a very long time (several tens of minutes even on a high spec PC), and depending on the size and complexity of the structure, can cause Perl to crash completely. Obviously this is not good. If the structure existed originally, it must be possible to reconstruct it too.

The eval method is still the easiest to read by a human, and, for trivial to mildly complex and large structures, the time taken for reconstruction is still acceptable. However, to overcome the inherent limitations of eval and also to speed up large structures another method had to be considered.

Solution - XML

The obvious way to me to get away from using eval, and still have for free parsing of the file and a clearly defined structure, was to use XML. Essentially, XML can be read in two ways, using a DOM parser, or using a SAX parser. The DOM parser goes away and reads the file in its entirety returning a fully reconstituted object representing the XML. The SAX parser is interactive, using callbacks to handle various tag constructs within the XML, so that the caller can do whatever they like with the data.

The DOM method means that the object structure would not be as the original without further processing, but would just reflect the XML. No good. So, I chose the SAX method as this would allow the reconstruction of the variable bit by bit, much like the way the original was built. Obviously, the XML adds a dependency on a parser, so make sure you have XML::Parser::PerlSAX available.

Code and Perl POD

It works really well, with structures of over 40 megabytes (XML file size that is) being persisted and reconstructed in 5 to 10 minutes. So, check out the Perl POD documentation and the code for this useful module.


DirWalk.pm

Directory walking is a handy capability to have in your box of coding tools. It comes in useful when wanting to perform actions on files in directories, or on the directories themselves. This module walks a directory structure, either top-down or bottom-up, and calls user defined functions for the files and directories found. The interface is simple and object based, so you can have many walkers active at the same time.

Code and Perl POD

I have used this module on both Windows and Unix versions of Perl and have found it very useful because of its simplicity. Check out the Perl POD documentation and the code for this useful module.


[Fiendish Home]


Content of this page Copyright © Robert Quince 1996 - 2005.
Site Comments