...Mea Culpa, Mea Culpa, Mea Maxima Culpa!. :-)

I quite liked you description :)

So here is the

zentara package/module tutorial

a package (ZenTara) is inlined with the main source code

zen-pack.pl

#!/usr/bin/perl -- use strict; use warnings; ZenTara::hello(); ZenTara::goodbye(); exit( 0 ); package ZenTara; sub hello { print "\nHello\n"; } sub ZenTara::goodbye { print "\nGoodbye\n" }

a module is when you put the package into a separate file

ZenTara.pm , save in current directory or in ... site/lib/ZenTara.pm

package ZenTara; use strict; use warnings; sub hello { print "\nHello\n"; } sub ZenTara::goodbye { print "\nGoodbye\n" } 1; ## return true

zen-mod.pl is the perl program that does not inline but instead uses ZenTara

#!/usr/bin/perl -- use strict; use warnings; use ZenTara; ZenTara::hello(); ZenTara::goodbye(); exit( 0 );

less typing by importing functions from ZenTaraE; also making objects

zen-mod-exp.pl imports hello/goodbye from ZenTaraE, also creates an object

#!/usr/bin/perl -- use strict; use warnings; use ZenTaraE; hello(); ZenTaraE->new( name => "Bob" )->goodbye(); exit( 0 );

ZenTaraE.pm uses Exporter to export hello/goodbye by default, and has object constructor "new"

package ZenTaraE; use strict; use warnings; require Exporter; our @ISA = qw/ Exporter /; our @EXPORT = qw/ hello goodbye /; # exported by default sub new { my $class = shift; return bless { @_ }, $class; } sub hello { print "\nHello\n"; } sub goodbye { my( $self ) = @_; my $name = eval { $self->{name} } || ''; print "\nGoodbye $name\n" } 1; ## return true

rip modules apart and insert them directly into your script

zen-mod-exp-inlined.pl inlining comes first before main program with %INC trick to make ZenTaraEI useable

#!/usr/bin/perl -- BEGIN { $INC{'ZenTaraEI.pm'} = __FILE__; package ZenTaraEI; use strict; use warnings; require Exporter; our @ISA = qw/ Exporter /; our @EXPORT = qw/ hello goodbye /; # exported by default sub new { my $class = shift; return bless { @_ }, $class; } sub hello { print "\nHello\n"; } sub goodbye { my( $self ) = @_; my $name = eval { $self->{name} } || ''; print "\nGoodbye $name\n" } 1; ## return true }########## use strict; use warnings; use ZenTaraEI; hello(); ZenTaraEI->new( name => "Bob" )->goodbye(); exit( 0 ); exit( 0 );

See how they run

$ perl zen-pack.pl Hello Goodbye $ perl zen-mod.pl Hello Goodbye $ perl zen-mod-exp.pl Hello Goodbye Bob $ perl zen-mod-exp-inlined.pl Hello Goodbye Bob $

For more info see Simple Module Tutorial and perlmod


In reply to Re^4: package vs module? (zentara package/module tutorial) by Anonymous Monk
in thread package vs module? by littlemonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.