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

Hi

Do tell me the difference between using

use <package_name> qw( )
use base qw(<package_name>)

Regards
Priya

Replies are listed 'Best First'.
Re: How to use a 'Use'
by BaldPenguin (Friar) on Jul 05, 2005 at 07:05 UTC
    Using a package does just that, it uses it. However using base , uses the package and adds it to the @ISA so that it the packages are inherited.

    From the documentation:
    package Baz; use base qw(Foo Bar);
    Is equal to
    package Baz; BEGIN { require Foo; require Bar; push @ISA, qw(Foo Bar); }

    Don
    WHITEPAGES.COM | INC

    Edit by castaway: Closed small tag in signature

Re: How to use a 'Use'
by polettix (Vicar) on Jul 05, 2005 at 07:49 UTC
    And, just in case you're wondering, the doc is here (you can also use perldoc base).

    From a certain point of view, the question is not correct - in both cases, the keyword use means exactly the same thing.

    1. use <package> <list of items>; 2. use base <list of parent modules>;
    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:

    use <package> qw();
    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
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
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)

    use Some_Module qw( blah );
    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)

    use base qw( Some_Package );
    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:

    use some_pragma qw( foobar );
    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

    use base qw( Some_Package );
    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