Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Template to create modules

by guiwp (Sexton)
on Jun 14, 2016 at 15:35 UTC ( #1165601=perlquestion: print w/replies, xml ) Need Help??

guiwp has asked for the wisdom of the Perl Monks concerning the following question:

I found two post very useful describing modules creation from a template. I've a doubt about which of them is better:

The first source that I've found is from: Re: Simple Module Tutorial

package MyModule; use strict; use warnings; use diagnostics; use Carp; ...

But surfing on the web I've found this one: template-boilerplate-code-create-perl-module

package DevDaily::Time; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(get_time_suffix); # symbols to be exported by defa +ult (space-separated) our $VERSION = 1.00; # version number ...

What is more appropriate for perl version 5.20.2 (default perl on debian in jessie)?

Replies are listed 'Best First'.
Re: Template to create modules
by choroba (Archbishop) on Jun 14, 2016 at 15:45 UTC
    It depends on what you want to do with the module. diagnostics is for newbies only, it probably shouldn't stay in the module once published. Carp is nice, but not always desirable, so add it only if needed.

    The second template misses strict and warnings, and its way of using Exporter is clunky. I'd use something like the following instead:

    use Exporter qw{ import }; our @EXPORT_OK = qw( blah ... );

    Also note that the module doesn't need Exporter if it's object oriented.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Template to create modules
by stevieb (Canon) on Jun 14, 2016 at 16:38 UTC

    This appears to be an XY Problem. Can you describe what the objective/purpose/use cases of your module are?

    I usually use Module::Starter to create a shell module. It actually creates a full blown distribution. Here's an example:

    $ cpanm Module::Starter # Module::Starter installs the module-starter binary: $ module-starter --module="MyModule" --author="My Name" --email="my@em" --eumm $ cd MyModule $ ls Changes ignore.txt lib Makefile.PL MANIFEST README t xt

    Now, you can keep the whole distribution intact (so you can run the likes of tests, and make install etc, but you can also take just the lib/ file out and use it standalone.

    The result is a module base that you edit to your needs. It includes a POD section and everything. Just put your code under the use warnings; line.

      Wow, that's is nice, I'll test it @stevieb. I like to be practical and it looks practical!

      Can you describe what the objective/purpose/use cases of your module are?

      First I want to be able to create a module following the principle of KISS (Keep It Short and Simple). A starting point for my first modules.

Re: Template to create modules
by Marshall (Canon) on Jun 14, 2016 at 15:44 UTC
Re: Template to create modules
by Discipulus (Abbot) on Jun 15, 2016 at 07:25 UTC
      Very useful @Discipulus, thank you. While reading some I found one interesting tool: h2xs (found in the Josť's Guide). The Module::Starter pointed out by @stevieb is more flexible but h2xs can be a good starting point too as it comes by default with my perl distribution.
Re: Template to create modules
by guiwp (Sexton) on Jun 14, 2016 at 16:27 UTC

    Thank you both for suggestions.

    Btw I've read some man pages and I found perlmod and it has one template:

    $ man perlmod |sed -n "/package Some::Module/,/^ \+1; /{p}" package Some::Module; # assumes Some/ use strict; use warnings; BEGIN { require Exporter; # set the version for version checking our $VERSION = 1.00; # Inherit from Exporter to export functions and variabl +es our @ISA = qw(Exporter); # Functions and variables which are exported by default our @EXPORT = qw(func1 func2); # Functions and variables which can be optionally expor +ted our @EXPORT_OK = qw($Var1 %Hashit func3); } # exported package globals go here our $Var1 = ''; our %Hashit = (); # non-exported package globals go here # (they are still accessible as $Some::Module::stuff) our @more = (); our $stuff = ''; # file-private lexicals go here, before any functions which + use them my $priv_var = ''; my %secret_hash = (); # here's a file-private function as a closure, # callable as $priv_func->(); my $priv_func = sub { ... }; # make all your functions, whether exported or not; # remember to put something interesting in the {} stubs sub func1 { ... } sub func2 { ... } # this one isn't exported, but could be called directly # as Some::Module::func3() sub func3 { ... } END { ... } # module clean-up code here (global destr +uctor) 1; # don't forget to return a true value from the file

      Looking at the examples given surprise me one thing: just one of the samples gave me this "BEGIN" and "END" (the one provided through the perlmod).

      I've read now one article about this thing called BEGIN and it says on one comment:

      "You didn't mention that BEGIN blocks even run when syntax checking with perl -cw. That's always seemed worrying on a security basis to me - I'm checking the syntax of the program, not running it! What if the BEGIN block does system( "rm -rf ...")?"

      So trust on code that has this feature is a bit dangerous when you want to just syntax check...

      For the curiosity I've checked if the library that I'm using to learn perl use this feature and it doesn't! (see here).

        > I've checked if the library that I'm using to learn perl use this feature and it doesn't!

        Sure it does. You need to follow the use clauses, line #11 says

        use JSON;

        which in turn, on line #9, says

        BEGIN {

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Template to create modules (nonethat)
by Anonymous Monk on Jun 14, 2016 at 23:57 UTC

    Yeah, forget about templates, don't add stuff until you know why you're adding it, that way you don't need a template

Re: Template to create modules
by shawnhcorey (Friar) on Mar 03, 2017 at 13:40 UTC

    The hash, `%EXPORT_TAGS`, should be included, especially the tag `:all`.

    # -------------------------------------- # Exports use Exporter qw( import ); our @EXPORT = qw( ); our @EXPORT_OK = qw( ); our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], );

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1165601]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-01-20 17:57 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (57 votes). Check out past polls.