Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Template to create modules

by guiwp (Sexton)
on Jun 14, 2016 at 16:27 UTC ( [id://1165616]=note: print w/replies, xml ) Need Help??


in reply to Template to create modules

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/Module.pm 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

Replies are listed 'Best First'.
Re^2: Template to create modules
by guiwp (Sexton) on Jun 15, 2016 at 00:04 UTC

    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,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found