in reply to Building in a Subroutine?
The Perl POD is a little "head first" with respect to introducing modules. perlmod is not a tutorial. This is where a book like Intermediate Perl (Published by O'Reilly) is very helpful. It will walk you through the basics of modules, and then take you into more advanced topics such as putting together a module distribution (tests and all).
But the basic idea is that you create a module in its own file. It has its own namespace. You can choose to export some of its functions into the caller's namespace, or make exporting optional. You can write a functional interface, or an object oriented interface, or a combination of both. Within the calling script, you use ModuleName;, along with an import list if necessary. Then within the calling script, you can use those functions (or classes/objects) without copying and pasting every time you want their functionality.
there are lightweight techniques too, but they're rarely used nowadays in well-written code; techniques such as reading in a file of code and evaling it, or do Fildname;, and others. But in this day and age, such techniques are not really considered suitable 99.9% of the time. I mention them only because they show up in older code from time to time, and you may see them.
Grab a copy of Intermediate Perl at your favorite bookstore. If you intend to become effective in using Perl, it's $20 dollars well spent.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Building in a Subroutine?
by perl.j (Pilgrim) on Jul 08, 2011 at 20:02 UTC |