Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Write a Perl module from scratch

by Anonymous Monk
on Oct 13, 2008 at 09:47 UTC ( [id://716774]=perlnews: print w/replies, xml ) Need Help??

Linux Format has a short guide to writing a Perl module, ideal for those who've dabbled with the language and want to make their code more re-usable and easier to maintain.

Replies are listed 'Best First'.
Re: Write a Perl module from scratch
by cog (Parson) on Oct 13, 2008 at 09:53 UTC
Re: Write a Perl module from scratch
by perrin (Chancellor) on Oct 14, 2008 at 01:51 UTC
    Don't follow this guide. h2xs is no longer the best way to start pure-perl modules. Use something like Module::Starter instead.
        It's more up-to-date with current practices for CPAN. It can generate Module::Build support, makes better starter tests, generates the YAML file, has stub POD that's more useful for modern OO modules, etc. h2xs also generates some strange file layouts, as discussed here in the past.
      Better yet, if you want to write more than about 2 modules, just write your own. h2xs adds more useless junk with every Perl release, and Module::Starter seems no better. When you write your own, you type in all and only the boilerplate you want when you write it, and you don't have extraneous garbage inserted when you create future modules. Here's what I use:
      #!/usr/bin/env perl use Getopt::Long; $ME = q|WHATEVER|; sub usage { print STDERR <<'EOS'; Usage: newmod.pl [-u 'Name <user@host>'] Module::Name EOS exit 1; } GetOptions('user:s' => \$ME) && @ARGV == 1 or usage; $mod = shift; ($dir = $mod) =~ s/::/-/g; mkdir $dir or die $!; chdir $dir; sub cat { my $o = shift; open OUT, ">$o" or die $!; print OUT $_ for @_; close OUT; } ($file = $mod) =~ s/.*:://; cat 'Makefile.PL', <<EOS; use ExtUtils::MakeMaker; WriteMakefile( NAME => '$mod', VERSION_FROM => '$file.pm', PREREQ_PM => { }, (\$] >= 5.005 ? ## Add these new keywords supported since 5.00 +5 (AUTHOR => q|$ME|) : ()), ABSTRACT => 'Something.', ); EOS cat "$file.pm", <<EOS; package $mod; =head1 NAME $mod -- Whatever it does... =head1 SYNOPSIS =cut \$VERSION = '0.01'; 1; __END__ =head1 DESCRIPTION =head1 AUTHOR ${ME} Bug reports welcome, patches even more welcome. =head1 COPYRIGHT Copyright (C) $y $ME. All rights reserved, some wrongs reversed. This module is distributed under the same terms as Perl itself. =cut EOS cat "test.pl", <<EOS; use Test::Simple tests => 1; use $mod; ok(1, 'loaded'); EOS (undef, undef, undef, $d, $m, $y) = localtime; $y += 1900; cat "ChangeLog", <<EOS; $y-$m-$d $ME * original version; created by newmod.pl EOS cat "README", <<EOS; $mod version 0.01 INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install DEPENDENCIES COPYRIGHT AND LICENSE Copyright (C) $y, $ME This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. EOS cat 'MANIFEST', <<EOS; Makefile.PL MANIFEST README test.pl $file.pm EOS
        That looks fine, but Module::Starter is totally customizable. Search CPAN for examples of what others have done.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlnews [id://716774]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found