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

What is module? Difference between module and package?

by shans (Initiate)
on Oct 28, 2005 at 07:28 UTC ( #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

Comment on What is module? Difference between module and package?
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/' 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/ 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'



Re: What is module? Difference between module and package?
by ioannis (Vicar) 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?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-28 17:56 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (182 votes), past polls