in reply to Can't inherit subroutines - workaround?

...it seems myHuman knows nothing about the subclass Class::PObject::DBM from which it gets its save().

I spent a few minutes poking around the internals of Class::PObject trying to find some helpful documentation, but was a bit confused -- you mention a "subclass Class::PObject::DBM" but the PObject documentation seems to indicate that instead of inheriting, it should delegate to a separate Class::PObject::Driver::DBM object.

Are you using the most recent version of Class::PObject?

I'm also confused becasue PObject doesn't seem to have a new constructor, it constructs a new instance using a subroutine 'pobject()' which I think is the source of all my woes.

The Class::PObject docs clearly show the use of a constructor as "$person = new Person();"; the "pobject()" subroutine is for initializing a class not a single instance.

  • Comment on Re: Can't inherit subroutines - workaround?

Replies are listed 'Best First'.
Re: Re: Can't inherit subroutines - workaround?
by Anonymous Monk on May 08, 2004 at 21:17 UTC
    Thanks for looking Simon, you are quite right, I was muddled about the constructor, of course pobject makes the class and its instantiated with the usual new().

    Can you explain a little bit more what you mean by 'delegate' in a Perl sense, because the save() method I am after is, being file/system specific, somewhere in that Class::PObject::Driver::DBM I presume.

    When I dump the keys and methods that I get with myHuman I see everything intrinsic to myHuman, and the columns hash from PObject, but the save() method has got lost.

    Iyho would I be better _deriving_ a new class (however one does that in Perl) or hacking about with import / exports or just writing my own storable object from the top.

    I just found a snippet that lets me recurse up through all the parents @ISAs so it might help me debug where the reference to save is getting lost.
    btw, yes its the latest PObject I could find installed less than a week ago. Thanks Simon,
    Andy
      Can you explain a little bit more what you mean by 'delegate' in a Perl sense, because the save() method I am after is, being file/system specific, somewhere in that Class::PObject::Driver::DBM I presume.

      I mean that a reference to a driver object is stored within each PObject class's $props->{driver}, and that calling save on an individual object is implemented by making a call to that driver's save method to do the actual work.

      The initialization is done in Class::PObject's pobject() method, which also causes your class to inherit from Class::PObject::Template, which is where the main save() method is located; it in turn calls the save() method in Class::PObject::Driver::DBM.

      Iyho would I be better _deriving_ a new class...

      You've already done that, by declaring that @ISA = ("myStorableBase");.

      Are you creating your MyHuman objects correctly, with MyHuman->new()?

        I see. I have followed that to the best I can, and I am looking deeper into the PObject code. Actually the save() method does seem to be here after all. Doing a class listing with stvns* recursing class lister I get,

        MyHuman ------------------------------------------- humanthingysub myStorableBase ------------------------------------------- name phone pobject email parentthingysub id Class::PObject::Template ------------------------------------------- errstr remove __props save logtrc new load fetch count confess () columns dump remove_all ("" get set logconfig __driver croak carp drop_datasource

        I've labelled human and parent thingies which are markers for methods in places I want to extend. So, we do inherit from ::Template and we get save() into the bargain, but unlike using an instance at the top level which saves and loads just fine I get a runtime error (which I've been seeing as a missing save() all along). Something else is up...OK, the context of use is,
        #!/usr/bin/perl use MyHuman; my $person = MyHuman->new(); $person->name('bob'); $person->email('bob@bob.com'); $new_id = $person->save() or die "save bork"; my $newperson = MyHuman->load($new_id) or die "reload bork";; print "reload OK\nname is: ".$person->name()."\n";


        ..and we get a runtime,

        Can't locate Class/PObject/Driver/.pm in @INC...

        So my understanding is that its creating an instance just fine.Somehow the definition of the db_file and path are getting lost?
        ___________________________
        *  A little while back another monk posted a very useful tool which has been invaluable to me experimenting with more adventurous OO Perl. I can't find the link, even in the supersearch, so I've posted the code again below. Note where I am using this to explore the inheritance of class myHuman.

        #!/usr/bin/perl use strict; use warnings; no strict 'refs'; =pod # Class/Object Method Lister # on Jan 18, 2004 at 21:03 = by stvn # if you want certain methods ignored, # put their names in this string =cut my $IGNORE = ""; require MyHuman; printFlatClassView("MyHuman"); print "\n"; printExpandedClassView("MyHuman"); print "\n"; ## ------------------------------------------------------------------ sub getClassMethodList { my $class = shift; return grep { !/^($IGNORE)$/ && defined &{"${class}::$_"} } keys %{"${class}::"}; } ## ------------------------------------------------------------------ sub getCompleteClassMethodList { my ($class) = @_; my @methods = getClassMethodList($class); push @methods => map { getCompleteClassMethodList($_) } @{"${class}::ISA"}; return @methods; } ## ------------------------------------------------------------------ sub printExpandedClassView { my ($class, $tab_count) = @_; $tab_count ||= 0; if ($tab_count) { my $t = ("\t" x $tab_count); print "${t}$class\n"; print "${t}-------------------------------------------\n$t"; print join "\n${t}", getClassMethodList($class); print "\n"; } else { print("$class\n"); print "-------------------------------------------\n"; print join "\n" => getClassMethodList($class); print "\n"; } map { printExpandedClassView($_, $tab_count + 1) } @{"${class}::ISA"}; } sub printFlatClassView { my ($class) = @_; print("$class\n"); print "-------------------------------------------\n"; my %method = map { $_ => 1 } getCompleteClassMethodList($class); print join "\n" => keys %method; print "\n"; } ## ------------------------------------------------------------------ 1;
        I've retititled more approppriately. I do appreciate all your help with this.
        Andy.