palette has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to use a 'Use'
by BaldPenguin (Friar) on Jul 05, 2005 at 07:05 UTC | |
From the documentation: Is equal to
Don WHITEPAGES.COM | INC Edit by castaway: Closed small tag in signature | [reply] [d/l] [select] |
|
Re: How to use a 'Use'
by polettix (Vicar) on Jul 05, 2005 at 07:49 UTC | |
From a certain point of view, the question is not correct - in both cases, the keyword use means exactly the same thing. Do you see it? The second form is a specialisation of the first, where you're actually using package base, which you'll find among other CORE package files in your library. So, you'll be able to find a base.pm file, which will enlighten you with the real mechanism under the hood. The same applies when you use warnings, or use strict: you're asking to include the corresponding file, which encapsulates the requested functionality. These kind of modules are called pragmas. One final note. When you write: you're also specifying an empty list of items. This list is usually used by the module to understand what functions should be exported and what not, so you're asking to import nothing. This semantic is consensual for "normal" modules but not mandatory; as a matter of fact, pragmas use the list for completely other purposes (e.g. the base pragma grabs the list of ancestors from it). Flavio | [reply] [d/l] [select] |
|
Re: How to use a 'Use'
by tlm (Prior) on Jul 06, 2005 at 03:18 UTC | |
They look superficially similar, but semantically they mean very different things. The first line (with some modifications) loads the module in the file Some_Module.pm (in some directory listed in @INC), and then invokes Some_Module->import( 'blah' ), which typically (though not always) has the effect of importing blah into the current namespace. And all this happens at compile time. The second line (again with some modifications) has the effect of adding the string 'Some_Package' to the @ISA array of the current package. This amounts to turning the current package into a subclass of Some_Package. This too happens at compile time. The first line represents a far more general situation than does the second one. Lines like the second line are meaningful only when defining an OO class, while lines like the first one are ubiquitous, since they are used to load Perl code defined in other files. Here's a third variant: which looks very similar to the first line discussed above, but its primary intent is to provide compiler directives (possibly parametrized by some argument, such as the string 'foobar' in this example). Such "compiler directive" modules are called "pragmas" or "pragmatic modules". Pragmatic modules are very common too (e.g. strict and warnings). In fact happens to be an example of this, in which the pragma is base, the parameter is the string 'Some_Package', and the resulting compiler directive is something like "treat the current package as a subclass of Some_Package". the lowliest monk | [reply] [d/l] [select] |