Perl Module Primer (1/5)
[next] |
Perl Module Primer
By Dan Ragle
One of the earliest lessons we learn as beginning programmers--in any language--centers around the virtues of code reuse; that is, designing our scripts and applications such that the parts that are repetitive in nature are coded only once, with that single instance of code then reused as necessary throughout the remainder of the application. Designing code to be reusable, we are often--and rightly--told, pays dividends in the long run; since we save ourselves large amounts of time both in initial development (a single instance of code needs to be fully tested only once, as opposed to multiple "copies" of said code that would need to be tested separately with each instance); and, more importantly, in maintenance time for the application down the road. With our properly designed reusable code we can alter the functionality of multiple areas of our application in the future (all those areas that utilize the reused code block) by simply changing--and testing--that one area of script.
Pretty much all Perl coders--even novices--are familiar with the Perl module, a primary means for implementing reusable code blocks in the Perl language. The popular Comprehensive Perl Archive Network, or CPAN contains--as of this writing--over 7,800 separate modules that are ready to be plugged into your Perl application, providing nearly instant functionality ranging from ready-made interfaces to popular mainline applications such as Apache through socket-based network application development, database interfaces, and compression algorithms; just to name a few of the categories. Using a tested, already available module speeds our application development tasks by helping us to avoid "reinventing the wheel."
What is rare, however--especially among novices--are those that have actually written and used their own Perl modules. This is unfortunate; since modules provide a terrific means to not only avoid reinventing the wheel when it comes to specific functional areas but they also provide a way for us to reuse our own code segments in larger, multi-script applications. A well designed sub-routine or function does a fine job of allowing us to reuse code within a single script; but a module can take that same premise a step further and provide us with an elegant way to reuse a collection of functions--individually or in their entirety--across a large collection of separate scripts. If you've ever found yourself having to edit multiple scripts to make similar changes to a particular function or action that appears within each of them, then you would have benefited (and hopefully will, in the future) from the use of modules.
I suspect the reason that many Perl novices avoid developing their own modules is because they (modules) are seen as belonging to the realm of the expert coders. After all, most of those modules posted on CPAN were developed by programmers with years of experience--often in multiple languages. Additionally, module development may also be seen as requiring extensive knowledge in Object Oriented Programming (OOP), another topic that is often avoided by novices.
While it is true that modules (and more specifically, packages; we'll discuss the differences a bit later) provide key architectural support for OOP based development in Perl, there is no rule that says one must be an OOP guru to effectively develop and use them. Indeed, some modules provide no OOP-based functionality at all; they are simply a collection of reusable functions that can be easily accessed and utilized by multiple scripts.
And as to the first potential complaint--that modules belong solely in the realm of expert coders--that is simply not the case. In this article, we'll examine the basic building blocks and fundamental concepts that you'll need to know to begin writing and using your own modules. We'll attempt to focus this discussion specifically for Perl novices; those who know basic Perl syntax and preferrably have at least a script or two under their belt, but have heretofore avoided module development for either of the two reasons I described above (or for any other reason, for that matter). We'll touch--very briefly--on extended topics such as autoloading and object classes near the end; but our primary focus will be on the basic creation and implementation of modules.
With this background information in mind, let's begin by having a look at a simple "Hello World" module in Perl.
[next] |
Created: April 7, 2005
Revised: April 7, 2005
URL: https://webreference.com/programming/modules/index.html