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

does perl support inheritance?

for example, i have a container class

mat::User

which basically just contains a set of attributes(first,last)/getters and setters

and i have another class

mat::Session

which does authentication with a XML based api. i want to create a

mat::Session::User

that has functions that are added to the 'mat::User' that can only be accessed if you have a valid Session.

a sample use case would be

my $x=mat::User->new{first=>'foo', last=>'bar'};
mat::Session::User->create($x);

where the 'create' method uses the XML api to create a user 'foo bar' using the already authenticated mat::Session... mat::Session::User would need to inherit mat::Session as well as mat::User.

Replies are listed 'Best First'.
Re: Perl Module Inheritance
by blue_cowdawg (Monsignor) on Aug 29, 2007 at 17:59 UTC
        does perl support inheritance?

    Sort of. Take a look at perltoot and seek out the explaintion of what

    package myModule; use vars qw/ @ISA /; @ISA = qw/ otherModule /; # This is also an "otherModule"
    sort of thing does for you. There are limitations and you need to be sure to understand those limitations to minimize your frustrations.

    You can also do things like:

    package Foo; use base qw/ Bar Baz / ; |
    which is taken from the base man page. This also is a good read before proceeding further.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Perl Module Inheritance
by Joost (Canon) on Aug 29, 2007 at 19:27 UTC
    my $x=mat::User->new{first=>'foo', last=>'bar'}; mat::Session::User->create($x);
    Is all fine enough, but why would mat::Session::User need to inherit from mat::Session? I note that to get at the mat::Session from within mat::Session::User::create() you either need to make the mat::Session object available globally (i.e. using some sort of singleton class/function) or you'd need to pass in the session explicitly. Either way, there isn't any need to have mat::Session::User inherit from mat::Session, and it will probably be clearer not to inherit from the session at all.