Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: package vs module?

by zentara (Archbishop)
on Mar 26, 2014 at 10:18 UTC ( [id://1079795]=note: print w/replies, xml ) Need Help??


in reply to package vs module?

For a beginner level view, which gets you thru most cases, a package is used inline with the main source code; whearas when you want to put the package into a separate file, you call it a module. The nice thing about inline packages is that you don't need to worry about the location of the external file. Both ways allow you to create objects, or import functions and constants. It gets complicated, but the workings are very simple. As a matter of fact, you can take most conventional Perl modules and rip them apart into various packages and insert them directly into your script.

I know this is over-simplified, but that is the answer I would have wanted to hear when I was first learning.


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: package vs module?
by Anonymous Monk on Mar 26, 2014 at 10:24 UTC
      Thanks for providing the example link, but I posted my response as just a quick overview of what I see as the difference, in case the OP is not technically inclined enough to understand the Simple Module Tutorial. I remember when I first started to learn Perl, back around 1998, I didn't give a thought to what objects were, or how they got there, back in those days, most scripts were written mostly as a monolithic collection of functions. Objects only confused beginners back then.

      I also just passed up reading all the links you posted, and failed to realize that what I said, was basically the topic of the Simple Module Tutorial, but in far less words. . Mea Culpa, Mea Culpa, Mea Maxima Culpa!. :-)


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        ...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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found