Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

What is module? Difference between module and package?

by shans (Initiate)
on Oct 28, 2005 at 07:28 UTC ( [id://503583]=perlquestion: print w/replies, xml ) Need Help??

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

Module can have one or more package into it. Or one-one relationship between module and package? Give some example to clear my doubt.

Thx in advance
shan

  • Comment on What is module? Difference between module and package?

Replies are listed 'Best First'.
Re: What is module? Difference between module and package?
by polypompholyx (Chaplain) on Oct 28, 2005 at 07:44 UTC

    A module is a .pm file containing some Perl code. A package is a declaration of the name of the current scope, e.g.:

    package Math::Complex

    which can occur in the code of a module or a script. A module can be used, because it is a physical 'thing', but a package cannot. A module may contain several packages: CPAN does this. The advantage is that the code inside that module can still be chunked up into classes/etc. by package declarations, as if these classes were separate modules, but that the functionality is still lumped into a single convenient file.

      Please note that package is scoped:
      package Foo; print __PACKAGE__,$/; { package Bar; print __PACKAGE__,$/; } print __PACKAGE__,$/; __END__ Foo Bar Foo
      with the default scope being the file, or course (although you rarely have to think about things like that, fortunately).
Re: What is module? Difference between module and package?
by xdg (Monsignor) on Oct 28, 2005 at 08:44 UTC

    polypompholyx has it right. Some of your confusion may be related to the way that barewords are translated into filenames when Perl looks for a module. For example, 'use Math::Complex;' signals Perl to search for the file 'Math/Complex.pm' in @INC and to load it if found (or die with an error if it can't be found).

    But even if the module Math/Complex.pm is found, it may or may not contain the package declaration Math::Complex. For example, it could have a typo:

    package Math::Compelx; # blah, blah, blah 1; # modules need to return 'true'

    Or it could have multiple packages:

    package Math::Complex; # blah, blah, blah package Math::Complex::Private; # blah, blah, blah 1; # modules need to return 'true'

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: What is module? Difference between module and package?
by ioannis (Abbot) on Oct 28, 2005 at 08:57 UTC
    packages are namespace areas so that symbols with similar names do not clash with one another. For example, the symbol &main::first is different from symbol &List::Util::first . Packages are name prefixes to sysbols.

    A module is a file of code, or a tree of bytecodes. A module could be precompiled (.pmc), uncompiled (.pm) on disk; or pre-loaded in memory as one unit -- assuming no autosplit.

    In summary: packages are about namespaces, and modules are about files. They are different things, like apples and boxes -- until the day when you start placing one kind of apple into one kind of box, and people start thinking that apples and boxes are related. And they are! but still, one in fruit and the other is a type of container.

Re: What is module? Difference between module and package?
by gube (Parson) on Oct 28, 2005 at 09:07 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found