package a::b::c:d
my $res = a::b::c:e->new(); # i am doing like this
# is there any othere to do this
sub new {
...
my $self = {
#result = a::b::c:e->new();
}
}
sub test {
}
sub test2 {
}
1;
package a::b::c:e
sub new {
...
}
sub testresult {
}
1;
My question is:
-
how to initalize the
emodule indin new itself rather creating every function and -
how to use that to store some results into
e:testresult
There are two strategies — either examine the symbol table to initialize on creation, or make use of AUTOLOAD and test with
can. AUTOLOAD can be messier as you have to deal with the case where the method isn’t there:But the symbol table trick is brittle, as if they’re using inheritance, you won’t see the inherited methods without walking up @ISA, too. (and even if they’re not — they might start using inheritance in the future, which results in things breaking)
…
Typically, when you’re trying to copy another module’s interface, you’ve got a case of inheritance, so you might want to ask yourself what the relationship is between
::dand::e:a::b::c::dis ana::b::c::ea::b::c::dusesa::b::c::eIf it’s an is-a relationship, it’s typically better suited to inheritance (although you might have wrappers around each of the methods, and still need to go through this whole exercise anyway). If it’s a uses relationship, odds are you don’t want to inherit from every last method they have, and can just hard code a list (although, the list might change if the used class is updated)