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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.