How do you write a module for Perl? In Python you can use:
# module.py
def helloworld(name):
print "Hello, %s" % name
# main.py
import module
module.helloworld("Jim")
How do you write a module for Perl? In Python you can use:
# module.py
def helloworld(name):
print "Hello, %s" % name
# main.py
import module
module.helloworld("Jim")
A class:
# lib/Class.pm
package Class;
use Moose;
# define the class
1;
A module that exports functions:
# lib/A/Module.pm
package A::Module;
use strict;
use warnings;
use Sub::Exporter -setup => {
exports => [ qw/foo bar/ ],
};
sub foo { ... }
sub bar { ... }
1;
A script that uses these:
# bin/script.pl
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use Class;
use A::Module qw(foo bar);
print Class->new;
print foo(), bar();
Basically you create a file named Yourmodulename.pm
, whose contents are:
package Yourmodulename;
# Here are your definitions
1; # Important, every module should return a true value
Then the program that uses the module will look like:
#!/usr/bin/perl
use strict; # These are good pragmas
use warnings;
# Used modules
use Carp; # A module that you'll probably find useful
use Yourmodulename; # Your module
You may want to organize your modules in a hierarchical (and hopefully logical) way. To do so you create a tree of directories like:
Your/Module.pm
Your/Other/Module.pm
And then in your program:
use Your::Module;
use Your::Other::Module;
There are more facilities to export functions and variables from your module, you can take a look at Henning Koch's "Writing serious Perl: The absolute minimum you need to know".
An "exact" equivalent of your Python example in Perl would look like this:
# MyModule.pm
package MyModule;
sub helloworld {
my ( $name ) = @_;
print "Hello, $name\n";
}
1;
# main.pl
use MyModule;
MyModule::helloworld( 'Jim' );
For more, see the entry for package
in perlfunc documentation. For much more, see the perlmod documentation.
/I3az/
One minor detail that the answers so far haven't mentioned is that, if you have a (preferably small) module which is purpose-specific enough that it will never be reused, you can put it into the same file as the main program or another package:
# main.pl
# Since this is a beginner question, I'll also point out that you should
# *always* use strict and warnings. It will save you many headaches.
use strict;
use warnings;
MyModule::helloworld('Jim');
AnotherModule::helloworld('Jim');
package MyModule; # Still in main.pl!
sub helloworld {
my ( $name ) = @_;
print "Hello, $name\n";
}
package AnotherModule; # Yep, still main.pl
sub helloworld {
my $name = shift;
print "Another hello to $name\n";
}
This isn't used often because it gives you a package that's defined in a file whose name isn't the same as the package's, which can get confusing because you have to use
/require
the filename, but reference it in code by the package name.
Also note that the 1;
is only needed as the final line of each file which is included via use
/require
. In this case, I didn't need it because it's in main.pl
. If you put multiple packages into the same file, you only need a 1;
at the end of the file, not after each package.
The last third of Intermediate Perl is devoted to module creation.
Whenever you want to know how to do something in Perl, check perltoc, the table of contents for the Perl documentation:
% perldoc perltoc
Several parts of the core Perl documentation can help you:
Good luck,
The most traditional way of setting up a module is as follows:
package Foo::Bar;
our @ISA = qw(Exporter); # Tells perl what to do with...
our @EXPORT = qw(sub1 sub2 sub3); # automatically exported subs
our @EXPORT_OK = qw(sub4 sub5); # exported only when demanded
# code for subs, constants, package variables here
1; # Doesn't actually have to be 1, just a 'true' value.
and as others have said, you can use it like so:
use Foo::Bar;