| [reply] |
There are a good few conventions for making modules and,
perlmod is a bit too condensed to learn it IMHO, although
an excellent reference.
Sadly most of the knowledge
I've gleemed on this subject is in the pages of Debugging
Perl,
The Perl Cookbook and Object Oriented Perl, so I can't
readily submit URL's for the salient points, all these books
give a well rounded look at modules and their creation and
use, from different angles, with little overlap. If I had to
point to one book to use, for this and other reasons it'd be
the Cook Book. HTH.
Your code might benefit from indentation and you don't need
the brackets around the variables being declared.
You might lessen your future efforts further by making the
functions more generic, maintainable and robust:
- Passing the list of recipients as a array reference.
Try a closure so you can set up the recipients once. If it
is static.
- Consider whether prototypes are useful, in some cases you
can use them to enforce correct use of your functions.
- Add validation as far as possible. Unless this is purely
for your own benefit, it is polite to inform users of why
things have failed. So to that end consider returning the
results from the system call.
- Add Pod to the functions or perhaps good commenting,
examples of usage, and such.
You were asking for Module specific stuff, and I fear I may
be repeating things, you already know. A more precise set of
goals to enable you to get `moduled' are:
- Learn how to set the module in a remote and common
directory. @INC, use lib or -i
- Decide which functions are private or public. use Exporter and @Exporter
- Ensure your code is legible and well commented. Here is a good place to
begin.
- Look to CPAN to see if what you're doing has already
been done.
<Hint to="greater Perl Monks" type="pretty big one">It is funny that there is no tutorial for modules in Tutorials</Hint>
--
Brother Frankus. | [reply] |
The first thing you must do is read through the perlmod documentation so you understand exactly what it is modules do and how they interact with the calling program. I would also recommend strongly that you read the entry on the @INC variable in perlvar. Understanding this is also important.
After that, determine the directory structure and name for your module, like Mymods/Thismod.pm, create the file and place your subs within the skeleton below:
package Mymods::Thismod;
### YOUR CODE GOES HERE ###
1;
If you have read the documentation recommended then this will all come clear to you.
Update Make sure you read through frankus' post above too, all the comments he makes about your code are well worth following.
$japh->{'Caillte'} = $me; | [reply] [d/l] [select] |
If you don't want the trouble of converting your scripts to .pm modules, but you can share a bunch of subroutines across multiple scripts by 1) putting them in a separate file (say snmplib.pl) 2) adding a 1; to the end of the snmplib.pl file, and 3) putting a code fragment at the beginning of scripts which would call those subroutines like BEGIN {
require "/var/www/cgi-bin/snmplib.pl";
}
.
It's not as elegant as modules and it's not particularly mod_perl friendly, but it works and is imho quicker to implement. (It is true that global variables in the two files are accessible to each other with this approach, not encapsulated and filtered through well-defined EXPORT interfaces, but you may or may not care about that depending on how your code works.) Perhaps some more experienced monk will tell you (and me) why not to do it this simple way. | [reply] [d/l] |
| [reply] [d/l] |
| [reply] |