Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

The relation between a Package and a Module?

by theantler (Sexton)
on Apr 09, 2010 at 11:53 UTC ( [id://833765]=perlquestion: print w/replies, xml ) Need Help??

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

Hello folks - a question suddenly popped into my mind .. What is the relation between a Package and a Module in Perl, since they have a similar syntax:
Use File::find Package File; sub find { .. }
I read Coping with Scoping, wow what a great read .. I wish it had mentioned Modules as well. *'UPDATED : THANKS FOR THE ANSWERS! REALLY HELPFUL

Replies are listed 'Best First'.
Re: The relation between a Package and a Module?
by jethro (Monsignor) on Apr 09, 2010 at 12:17 UTC

    A module is just a package with some conventions to make it easy to use as a library. One convention is to use the package name as filename so the library can be found, and to use .pm as suffix. Also depending on the module something like Exporter is used to export some function- or variable-names into the namespace of the script that is using the module.

    See perlmod for more information

Re: The relation between a Package and a Module?
by moritz (Cardinal) on Apr 09, 2010 at 12:25 UTC
    I read Coping with Scoping, wow what a great read .. I wish it had mentioned Modules as well.

    That was outside the scope of that node :-)

    But really packages don't interact very strongly with scoping. A package declaration really doesn't affect lexicals in any way.

    package First; my $x; package Other: print $x; # still the same $x

    Since our declarations really just provide lexical aliases for global storage locations, they interact in the same with packages as lexicals do - not at all. (Only the global storage location depends on the current package).

    package First; our $x; package Other: print $x; # still the same $x

    Only the fact that many packages live in files on their own affect scoping: each file is a scope of its own.

Re: The relation between a Package and a Module?
by amir_e_a (Hermit) on Apr 09, 2010 at 12:19 UTC

    It's quite the same thing.

    "Package" is the technical name for what is often called a "namespace" in other languages. It can be used as an object-oriented class, or just as a module, which is simply a bunch of definitions, usually of functions.

    When talking about a module, especially a CPAN module, some kind of good order is often implied - a makefile, some documentation, a well-defined API, a test suite etc., although none of it is technically required.

Re: The relation between a Package and a Module?
by dsheroh (Monsignor) on Apr 10, 2010 at 11:17 UTC
    If I may quote from an Iron Man blog post I made last June:
    In the last couple weeks, I've seen Perl questions on at least two separate forums which revolved around the connection between .pm files and Perl packages. Really, though, this connection is quite easy to understand if you just remember one thing:

    It doesn't exist.

    Assigning the same name to a package and to the .pm file where that package resides is purely a matter of convention, adhered to for the sake of (human) readability. It is completely ignored by the language. If you want to put package Foo into Bar.pm and use it along with a package Bar which is split between Xyzzy.pm and Quux.pm, then you're perfectly free to do so. You'll confuse the hell out of yourself and whatever poor soul is condemned to maintain your code following the inevitable collapse of your sanity, but you can do it.
Re: The relation between a Package and a Module?
by Anonymous Monk on Apr 09, 2010 at 22:00 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found