XP is just a number | |
PerlMonks |
Re: difference between packages and moduleby tlm (Prior) |
on Jun 21, 2005 at 12:51 UTC ( [id://468649]=note: print w/replies, xml ) | Need Help?? |
Here are a few small examples, roughly ordered from most to least typical. 1 package (aka namespace) in 1 module
Actually, in this example there are two namespaces (Foo and main), but its principal point is to illustrate the case of one package (Foo) residing in one module (Foo.pm). (Actually, as ysth already alluded to, there is some ambiguity in the common usage of the word "module"; e.g. in some contexts one would talk about the "CGI module", referring to the distribution (which may contain several files); in other contexts one may speak of the "CGI.pm module", referring to a specific file in the distribution.) As a bonus, this example also illustrates that
Multiple packages (namespaces) in 1 module
Notice that in foo.pl we don't have the line In fact, perl would complain bitterly if we did: That's because require is ultimately looking for files. I.e., to a first approximation at least, as far as require is concerned, a module is a file. In fact, once require finds the file that it's looking for, it doesn't care whether the namespace(s) declared in the file correspond to the name of the module. For example:
Notice that in this case the module require'd (i.e. loaded) by foo.pl is Foo, and require looks for it in a file called Foo.pm in some directory mentioned in @INC. It doesn't matter that no package named Foo is mentioned in Foo.pm. 1 packages (namespaces) in several modules
Notice that in foo.pl we now have two require's: In other words, the fact that require is loading stuff in package Foo when it gets Foo.pm does not, by itself, bring in any other Foo code from other modules. If we had omitted the second require, then perl would fail with the error: On the other hand, a small change in Foo.pm would render unnecessary the second require in foo.pl (as long as one has the first one):
Now Foo.pm loads More_Foo.pm, which makes a single require Foo enough in foo.pl. the lowliest monk
In Section
Seekers of Perl Wisdom
|
|