Instead of a random directory structure, I prefer to create a proper module:
h2xs -AXc -n FooBar...which will create a FooBar directory that contains the following:
Changes MANIFEST Makefile.PL README lib- |-- Foobar.pm t/
Then, create a FooBar directory within the lib dir, and put your Bar.pm and Foo.pm in that. I use the FooBar.pm as a module that contains any methods that will be common to more than a single sub class. So, in Foo.pm and Bar.pm, I'd have:
use base qw( FooBar );
In FooBar, some of the common subs I'd have are a generic new(), database setup subs, configuration file setup subs etc. Then, unless Foo or Bar require specific, non-generic versions of these subs, they will automatically be inherited when use()ing FooBar::Bar and FooBar::Foo without having to recreate the subs in each module file.
All in all, with the directory structure above and with this setup you could have:
# lib/FooBar.pm package FooBar; sub new { my $class = shift; my $args = shift; my $self = bless { name => $args->{ name }, age => $args->{ age }, }, $class; } return $self; } # lib/FooBar/Foo.pm package FooBar::Foo; use base qw( FooBar ); # no new() sub, it's inhereted sub age { my $self = shift; my $args = shift; $self->{ age } = $args->{ age } if $args->{ age }; return $self->{ age }; } # lib/FooBar/Bar.pm package FooBar::Bar; use base qw( FooBar ); # no new again sub name { my $self = shift; my $args = shift; $self->{ name } = $args->{ name } if $args->{ name }; return $self->{ name }; } # foo.pl use FooBar::Foo; use FooBar::Bar; # don't need to 'use' FooBar here my $foo = FooBar::Foo->new({ name => 'stevieb', age => '35', }; my $current_age = $foo->age(); my $year_older = $foo->age({ age => 36, });
Because you have a proper module setup, simply:
perl Makefile.PL sudo make install
to begin using your modules system-wide
I hope this helps in some way
Steve
In reply to Re: Extending objects
by stevieb
in thread Extending objects
by punkish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |