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

Brethern,

A question and a question about the question.

What is the difference between 'use base (Exporter);' and 'use Exporter;'? I am modifying a CPAN module, Module::Grapher . In a previous thread, I was kindly advised to import a function and a hashref, rather than addressing them with an unweildy pacakge prefix. I found that the code says 'use base (Exporter);' but doesn't actually export anything. What I'm used to, and what I found in the Simple Module Tutorial, is 'use Exporter;'? Is there a difference?

Meta-Question: I tried to SuperSearch for 'use base'. Alas many of the answers I received looked like
  Re^3: confused about reading from database using an array
Was there a better way for me to search?

The 'use base' issue thread helped a little. But I'm still confused. The Module::Dependency code is object-oriented — and are some of the modules it uses are. Can I leave 'use base (Exporter)' in place and add 'use Exporter;'? Or should I comment it out?

throop

  • Comment on Twofer: 'use base' vs 'use'; and SuperSearch

Replies are listed 'Best First'.
Re: Twofer: 'use base' vs 'use'; and SuperSearch
by Joost (Canon) on Dec 14, 2006 at 15:26 UTC
      That makes base sound worse than it is. If you write:
      #!/usr/bin/perl use strict; use warnings; use base qw/Kjfnekjrn/;
      you get:
      Base class package "Kjfnekjrn" is empty. (Perhaps you need to 'use' the module which defines that package f +irst.) at tt.pl line 4 BEGIN failed--compilation aborted at tt.pl line 4.
      You're absolutely right that it isn't complaining that it couldn't find the file, but the error message is helpful and to the point, in my opinion.
Re: Twofer: 'use base' vs 'use'; and SuperSearch
by jdporter (Paladin) on Dec 14, 2006 at 15:29 UTC
    I tried to SuperSearch for 'use base'.

    For starters, you should enter something in the box labeled
    separate strings with. I often use a semicolon for this purpose.

    We're building the house of the future together.
Re: Twofer: 'use base' vs 'use'; and SuperSearch
by adrianh (Chancellor) on Dec 14, 2006 at 16:27 UTC
    But I'm still confused. The Module::Dependency code is object-oriented — and are some of the modules it uses are. Can I leave 'use base (Exporter)' in place and add 'use Exporter;'? Or should I comment it out?

    You don't need both. Doing  use base qw(Exporter) will load the module.

Re: Twofer: 'use base' vs 'use'; and SuperSearch
by leocharre (Priest) on Dec 14, 2006 at 17:56 UTC

    When you 'use base' you inherit. That is, think of it as, your present module becomes that module.

    Let's imagine we write CGI/Mor.pm :

    package CGI::Mor; use base 'CGI'; 1;

    Then when I do this:  my $cgi = new CGI::Mor;

    The object $cgi behaves exactly as if I had done:  my $cgi= new CGI; because your new package .. sort of.. cloned let's say- CGI.pm.

    If you do this:

    package CGI::Mor; use base 'CGI'; use strict; sub footermod { my $self = shift; my $arg = shift; $arg ||= 'default arg'; my $out = $self->p( $self->small('welcome to this message! '. $arg ) ); return $out; } 1;

    And then in your script:

    use CGI::Mor; my $cgi = new CGI:Mor; print $cgi->footermod;

    it would print "welcome to this message! default arg"

    You can see how useful this is to add new functionality to existing modules. Which is why it can be useful to make modules do few tasks very well.

Re: Twofer: 'use base' vs 'use'; and SuperSearch
by jbert (Priest) on Dec 15, 2006 at 10:49 UTC
    As others have pointed out, use base makes the named module a superclass of the current package (and so you should really be writing an OO module in that case).

    I'd just add that in OO terms, if you pull in a package with use base then you are declaring an is-a relationship. Your package is an Exporter, with some additional custom behaviour.

    If you pull in a module in order to make use of the facilities it provides, it is much more like a has-a relationship. If I write an OO module and use XML::LibXML it is quite likely that my object will have some object's from that module as data members.

    This isn't 100%, since you might make use of a module's facilities purely within one method and never save off a data member (e.g. use Dumper), but it can be a helpful way of thinking about it.

Re: Twofer: 'use base' vs 'use'; and SuperSearch
by sgt (Deacon) on Dec 15, 2006 at 09:55 UTC

    well 'use base qw(Exporter)' means that you inherit the functionality of exporter. In short you are writing another Exporter. Also the published syntax is 'use base LIST' where LIST is a list of strings; so technically 'use base (Exporter)' is not correct and I don't use it (it works if the bareword can be interpreted as a string which means you did not 'use strict;' naughty you! ;)

    'use Exporter' means that you will let users of your module "import" some symbols in their namespace. Best practices imply that your module is not OO (or should not be...).

    hth --stephan