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

Greetings!

This is somewhat a followup on the post I did here: 591709

Now the idea is basicly the following:
I have this structure:
Object - class toString() - method File - class, inherits Object open(:$file) - metod ( $file paramerer optional as it could have + been send using File's constructor) close() - method Stat - Inherits File getSize() - method getModTime() - method
Now, I wanna be able of doing:
use Object::File; my $file = File->new("bah.mp3"); my $size = $file->getSize();
Now, as you can see here I can do getSize() since the Stat object is supposed to be loaded in there aswell! With inheritance is quite easy. But how to solve this? Its supposed to go recursive down from where you create something. In this case from Object::File as everything below there is supposed to be the File objects functionality! (Object::File::*::*...)
The thing is that I shouldn't have to know that there is a Stat module really. Since as you can see in the older thread, there is a method to get all methods an object has. So, the idea is that one can just do:
use Object::File; use Data::Dumper; my $file = File->new("bah.mp3"); print Dumper($file->find_methods());
and voilá. You know see what you can do with the file!!! (Should I patent this? ;) )
I have done this so far in the Object (as I assume this is probably the best place to do this since this is ALWAYS run as its the top object).
package Object; use strict; use warnings; use lib "."; sub new { my ($class) = @_; my $self = { _objectname => $class, }; my @sub_modules = glob("./Object/" . $class . "/*.pm"); # Load sub modules for the class foreach (@sub_modules) { #print $_ . "\n"; my ($pluginName) = $_ =~ /(\w+).pm/; #print "Class: " . $class . "\n"; require "./Object/$class/$pluginName.pm"; } bless $self, $class; return $self; } sub toString { my $self = shift; return $self->{'_objectname'}; }
But this doesn't really add the functionality to the File as shown dumping out the data with the find_methods(); sub mentioned in the other thread. Well, atleast there is no reference to the methods within the object...

Ideas how to solve this?

Thanks,
Ace

Replies are listed 'Best First'.
Re: Loading Functionality From SubModules
by Joost (Canon) on Jan 13, 2007 at 19:46 UTC
    As you've noticed, inheritance isn't really a good match for this kind of thing. It looks like it would be a better match for mixins, if Stat's methods are going to be reused in another class (like Directory).

    Or you could achieve basically the same thing by having Directory and File inherit from FileSystemObject (i.e. Stat with a slightly more useful name)

    Also, why are you requiring modules every time someone instantiates an Object?

    update: fixed link

      Also, why are you requiring modules every time someone instantiates an Object?
      Woops.. Well, that was just a copy 'n paste from something I found here doing with plugins...

      Update: And good idea with the FileSystemObject! Now, this I did here is just an example... That I use to test on. As noticed one can surely redo the hiarchy of the objects to a better one. But we can have this one for now... I can change that later when I have the solution I wanna use. (the mixin seems pretty nifty, but I'm gonna wait to see if others have other nice solutions). As it seems, maybe Stat wasn't a good example, as that should probably be a parent to File and Directory objects, but anyway.. still need a solution for other cases. And besides, its little easier to expand downward. Else one have to modify all mobules that are supposed to go below you... it could be a mess...