Tuesday, August 15, 2006

Perl packages and modules

I've been working on Perl scripts that continue to grow. I decided that they were getting out of hand, and wanted to organize the scripts better by breaking out some functions into Perl modules. I am somewhat new to Perl, and although I do own the camel book, I found their discussion on modules to be less than helpful. After some internet searching, I found the correct way to do it. Here's the short-and-sweet version:

Let's say you have a module located here:
/home/myname/ver-8/Common/DBUtils.pm

Here's what DBUtils.pm looks like -

package Common::DBUtils;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
...
sub db_fun
{
...
}
1;
__END__


Here's a script that uses that function -

...
# points to root directory of your module
use lib "/home/myname/ver-8";
use Common::DBUtils;
...
my $thing = Common::DBUtils::db_fun();


Simple enough! But it's like learning how to work with package names and the classpath in Java. A bit of a pain, to start. I need to do some more reading on the subject because I saw some discussions that indicate there are other ways to go about this.

0 Comments:

Post a Comment

<< Home