September 2011 Archives

Initself's Retarded Guide to Building a Package

To build a package, use Module::Install, ya turkey!

Update: Per mstpan, one should not use Module::Install!

cpan Module::Install


Make a directory for your package to reside:

mkdir /home/you/My-Package


Put your module in the 'lib' directory. This is where all your code is going to live.

mkdir lib
mkdir My
touch My/Package.pm


Put your code in My/Package.pm, or copy the file you already wrote here.

Then, create a Makefile.PL in /home/you/My-Package:

use inc::Module::Install;

# Define metadata
name           'My-Package';
perl_version   '5.006';
version        '0.01';
license        'perl';

# Specific dependencies
test_requires  'Test::More'  => '0.42';

WriteAll;


Then, just run your new shiny file, pal!

perl Makefile.PL && make && make install && make clean && rm Makefile.old


While your at it, put that in a file called 'install.sh' so you can execute that each time you install it. You've gotta be root to run this sucker!

Update: Don't install to system root! Use plenv!

sudo sh install.sh


Afterwards, your directory will looks like this:

inc/  install.sh*  lib/ Makefile.PL  META.yml


You're installed!

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'

About initself

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