I would suggest perlmod and perlmodlib manpages. perlmodlib has a section "Guidelines for Module Creation", which might help. Anyway IMHO the greatest suggestion is (from perlmodlib manpage itself) "try to reuse the existing modules either in whole or by inheriting useful features into a new class" ... it is very likely that a module that does what you need already exists.
marcos | [reply] |
In perldoc perlmod there is a cut-and-paste running start of a new module called Some::Module.
Try it out.
/brother t0mas
| [reply] [d/l] |
If you want it to look really professional you should go to http://search.cpan.org and search through the documentation because if you are using the same functions over and over, chances are so did someone else. The use of modules makes your code look clean and professional.
--Joe | [reply] |
i know that i am reusing the same functions over and over
So what he (or she?) really needs is just a package, not really a fully-blown OO interface. Remember the KIS rule!
Let's say you have a function for logging and a function for carrying out some kind of calculation.
Make a file called whatever.pm, and in it include:
package whatever;
sub log {
# do logging
}
sub calculate_something {
# do calculations
}
return 1; # Perl insists on this :-)
So in your scripts, you can just do:
#!/usr/bin/perl
# Tells Perl to look in current dir for modules, may not
# be necessary.
unshift @INC, ".";
use whatever; # the file must have .pm ext for 'use'
whatever::log("Log file entry");
$answer = whatever::calculate_something();
| [reply] [d/l] [select] |
There are a few options, you have to decide what works for you.
First, build a library file, we'll call it functions.pl, it may look like this:
sub add {
my( $x, $y ) = @_;
return $x + $y;
}
sub subtract {
my( $x, $y ) = @_;
return $x - $y;
}
1;
Now, your program may look like this:
#!/path/to/perl -w
use strict;
$|++;
requre '/path/to/functions.pl';
print subtract( add( 3, 2 ), 1 );
Next, to build a Perl Module that @EXPORTs the functions:
package Functions;
use strict;
use Exporter;
use vars qw/@EXPORT @ISA/;
@ISA = qw/Exporter/;
@EXPORT = qw/add subtract/;
BEGIN {
import Functions @EXPORT;
}
sub add {
return $_[0] + $_[1];
}
sub subtract {
return $_[0] - $_[1];
}
1;
Our program may resemble this:
#!/usr/local/bin/perl -w
use strict;
$|++;
use lib '/path/to';
use Functions;
print subtract( add( 3, 2 ), 1 );
An object oriented version of the Perl Module:
package Functions;
use strict;
sub new { bless {}, shift }
sub add { shift; $_[0] + $_[1] }
sub subtract { shift; $_[0] - $_[1] }
1;
And our program:
#!/path/to/perl -w
use strict;
$|++;
use lib '/path/to';
use Functions;
my $f = Functions->new;
print $f->subtract( $f->add( 3, 2 ), 1 );
I would probably (in most cases) go with the OO Module, but you have to choose your path.
Enjoy!
--
Casey
| [reply] |
I think your wanting to look into OO (object orientated) programming, There are some great tutorials on this page and others, one of my favorite has to be
this one I learned alot from it, its rather detailed, and easy to understand.. | [reply] |