Re: "use base" or @ISA
by davido (Cardinal) on Aug 17, 2004 at 15:06 UTC
|
Not exactly the same thing. @ISA = qw/Exporter/; doesn't perform a 'require Exporter;'.
From the base POD: use base; "is rougly similar to":
BEGIN {
require Foo;
require Bar;
push @ISA, qw(Foo Bar);
}
I like use base; also because it runs under strictures without explicitly calling "use vars qw/@ISA/;", or declaring "our @ISA...". ...just a minor improvement, IMHO, but I prefer to use it.
| [reply] [d/l] [select] |
Re: "use base" or @ISA
by ikegami (Patriarch) on Aug 17, 2004 at 15:16 UTC
|
As an aside, I hate inhererting from Exporter. My class does NOT have an ISA relationship with Exporter! (And inheriting an autoload? ouch!) Below is an alternative.
Instead of:
use vars qw( @ISA @EXPORT_OK %EXPORT_TAGS );
BEGIN {
@ISA = qw( Exporter );
@EXPORT_OK = qw( ... );
%EXPORT_TAGS = ( ... ); # if necessary
use Exporter;
}
try:
use vars qw( @EXPORT_OK %EXPORT_TAGS );
BEGIN {
@EXPORT_OK = qw( ... );
%EXPORT_TAGS = ( ... ); # if necessary
require Exporter;
*import = \&Exporter::import;
}
Update: s/module/class/; | [reply] [d/l] [select] |
|
My module does NOT have an ISA relationship with Exporter!
Really? So, how would you explain the relationship? Your module exports; it is an exporter.
| [reply] |
|
My module does NOT have an ISA relationship with Exporter!
Really? So, how would you explain the relationship? Your module exports; it is an exporter.
I would say that the module "delegates to Exporter", or "uses Exporter".
Delegation and composition are perfectly valid implementation mechanisms, and there's no reason to view inheritance as the one true way of sharing functionality.
| [reply] |
|
|
|
oops, that was a typo or a freudian slip or something. I meant to say my class isn't an exporter. A dog (for example) is an animal, but a dog isn't an exporter (although the package in which dog is defined might be). That's just my way of looking at things, so use the style you like the most.
| [reply] |
|
I use Exporter::Lite. It is much lighter and has a clean interface (its import() is simply exported). It doesn't support tags, but then I never needed them anyway.
Exporter::Tidy is another alternative if you need advanced features (some of which Exporter doesn't even offer.)
Makeshifts last the longest.
| [reply] |
|
package YourModule;
use Exporter qw( import );
under the heading Exporting without inheriting from Exporter.
| [reply] [d/l] |
Re: "use base" or @ISA
by borisz (Canon) on Aug 17, 2004 at 15:04 UTC
|
I use both from time to time. A difference is that use base 'Exporter'; is like
require Exporter;
push @ISA, 'Exporter';
so you may save a line. For all the differences read perldoc base.
| [reply] [d/l] [select] |
Re: "use base" or @ISA
by dragonchild (Archbishop) on Aug 18, 2004 at 02:58 UTC
|
There is a subtle difference between the following:
use base 'Foo';
----
use Foo;
our @ISA = qw( Foo );
I always use the latter, because 'use base' does a require, not a use. This means that Foo's import() isn't called. Most people would consider this to be a good thing, but I don't like limiting the options for the modules I work with. This is Perl, after all.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] [d/l] |
Re: "use base" or @ISA
by adrianh (Chancellor) on Aug 18, 2004 at 11:15 UTC
|
| [reply] [d/l] [select] |