Initself's Retarded Guide to Creating a Package

Here's my definitive guide to creating a Perl package (non-OO) for retards.

# where package is My/Package.pm
package My::Package;

# You can put it in the root directory if you wish
# and refer to it just as Package.pm, but try create your own namespace 
# if you can
#
# package Package;

use warnings;
use strict;
use base 'Exporter';

# you will have your sub automatically accessible
our @EXPORT = qw(your_sub); 

# Or, you can choose to explicitly import your subroutine name like
# this in your calling code: use My::Package qw/your_sub/
#
# our @EXPORT_OK = qw(your_sub);  

sub your_sub {
  return "foo";
}

1; # return a true value at the end of your module to keep things happy

Here's how you use it.

use My::Package;
my $bar = your_sub(); # $bar is now 'foo'

Leave a comment

About initself

user-pic Perl is better than sliced bread. Way better.