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

I have asked this question before. However, I have made progress since last time. The basic concept is I have this module, and all its functions are written. However, I want it to return an object of another module with the functionality of my module as well. Damian Conway actually has something like this in his book, but when I use it I get funny errors. So perhaps I am doing something wrong in my module or perhaps I am doing something wrong in my code. Either way, I suspect our monks can tell me which it is.
## begin mymodule.pm package OtherModule::MyModule; use OtherModule; @OtherModule::MyModule::ISA = qw (OtherModule ); sub new { my $self = $_[0] -> SUPER::new(@_[1..$#_]); ${ $self -> ${ _count } }--; $self -> { _count } = \$_count; ${ $self -> ${ _count } }++; $self; } ### begin myscript.pl use OtherModule; use OtherModule::MyModule; our $object = OtherModule::MyModule -> new ( qw( foo bar baz bletch ) +);
the error messages I get that are pertinent are thus (where MP3::Napster is "OtherModule" and OpenNap.pm is "MyModule"):
Uncaught exception from user code: Undefined subroutine MP3::Napster::OpenNap::registry at /usr/l +ocal/libdata/perl5/site_perl/MP3/Napster/Base.pm line 29 MP3::Napster::Base::AUTOLOAD('MP3::Napster::OpenNap=HASH(0x39c +9c)', undef) called at /usr/local/libdata/perl5/site_perl/MP3/Napster +.pm line 398 MP3::Napster::disconnect('MP3::Napster::OpenNap=HASH(0x39c9c)' +) called at /usr/local/libdata/perl5/site_perl/MP3/Napster.pm line 88 MP3::Napster::new('MP3::Napster::OpenNap', '127.0.0.1:31337') +called at /home/alex/conf/lib.perl/MP3/Napster/OpenNap.pm line 76 MP3::Napster::OpenNap::new('MP3::Napster::OpenNap', '127.0.0.1 +:31337') called at skillz line 24 main::dacts() called at skillz line 127
Sorry, im sure that will be wrapped in the ugliest manner possible on most browsers. I gather I am having a problem with something called 'AUTOLOAD'? I'm just not sure how to make it happy. I get the feeling I am very close to being done here.

Thanks again, and sorry if I am repeating myself.

brother dep.

--
Laziness, Impatience, Hubris, and Generosity.

Replies are listed 'Best First'.
Re (tilly) 1: Reverse Inheritance Irritance
by tilly (Archbishop) on Mar 27, 2001 at 11:04 UTC
    One thing that tye and I agree on is that inheritance is used far more than it should be. Now I cannot say whether this has anything to do with your problem, but when you inherit from a module whose guts you do not know, you get their implementation and your assumptions about what you can and cannot do with objects are likely to break.

    But luckily it is possible to inherit the interface without running the risk of problems with the implementation. Here is a simple example showing how to do that:

    package parent; use strict; sub new { return bless {}, shift; } sub talk { shift; print @_; } 1; package child; use Carp; use strict; use vars qw($AUTOLOAD); sub new { my $self = bless {}, shift; $self->{hndl} = new parent(@_); return $self; } sub AUTOLOAD { $AUTOLOAD =~ s/.*://; if (UNIVERSAL::can("parent", $AUTOLOAD)) { eval " sub $AUTOLOAD { (shift)->{hndl}->$AUTOLOAD(\@_); }"; no strict 'refs'; goto &$AUTOLOAD; } else { confess("Method $AUTOLOAD not implemented"); } } 1; package main; my $thing = new child; $thing->talk("Hello world\n"); # In the interface $thing->bye(); # Oops.
    The trick is in AUTOLOAD. If you call a method that you have not defined it will see whether it can proxy it. If it can then it creates that method and goes to it. If it can't then it blows up.

    UPDATE
    Took out the check for DESTROY in AUTOLOAD. I could have sworn that it could be called implicitly but it wasn't when I tested? Ah well, live and learn.

    UPDATE 2
    I misunderstood the (non)output from the tests. DESTROY was being called after all. I just wasn't seeing the output from it.

      Inheritance is indeed overused and proxying is often a good way to work around it. The downside to that is that it eats up more memory, and runs slower.

      While that is usually not bigger and slower enough to be truly detering, in some cases (mostly when you have potentially *lots* of object, for instance in a DOM tree) though it's clearly counter-indicated. I've been planning for a while to look into the ex::interface module on CPAN which is supposed to allow for interface inheritance but I haven't found the time at moments when my brain is in a good enough state :-/

      PS: do you still have the code that made DESTROY not turn up in AUTOLOAD ? It should really be called explicitly... a code construct that doesn't force one to check for that would be interesting.

      -- darobin

        I was seriously confused about this. After a bit of debugging I figured it out though. Here is a simple example that shows what is going on:
        my $test = sub {bless {}}; $test->(); $test->(); $test->(); sub DESTROY { print "Calling DESTROY\n"; die "But you won't see this message\n"; }
        See it?

        DESTROY is called in an eval. Since my code was only putting out output within a confess, that output was trapped. So I saw nothing, the script kept on going, and I thought that DESTROY wasn't being called. When in fact it is...

        Mystery solved. :-)

Re: Reverse Inheritance Irritance
by dws (Chancellor) on Mar 27, 2001 at 11:05 UTC
    Unless you re-bless the object returned from SUPER::new(), you'll not be able to add your own behavior. Here's what that might look like when coupled with a couple of readability aids.
    ... preamble as before use strict; my $_count = 0; # count of instances created sub new { my $package = shift; my $self = SUPER::new(@_); # The class we inherit from counts its instances. # Since we're really creating one of us, adjust # its count downward, then point the object at # our counter. ${$self->{_count}}--; $_count++; $self->{_count} = \$_count; bless $self, $package; }
    Without that bless, any attempts in call member functions would cause the search for that function to begin at the parent (overridden) class. Not what you want if you're going to be adding your own behavior in a subclass.

    (I assume that you've examined the class you're overridding, and that swaping out $self->{_count} won't have any unfortunate side-effects.)