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

Hello PerlMonks,
I have a quick question on inheritance. Here is the my super class.

Package EventHandler; sub new { my $class = shift; my $self = { EVENT_ID => $event_count, EVENT_CAT => shift, EVENT_STATUS => "current", EVENT_COMPONENT => undef, EVENT_START_TIME => undef, EVENT_END_TIME => undef, EVENT_STATE => "Unknown", EVENT_START_LOCATION=> undef, EVENT_END_LOCATION => undef, EVENT_RECOVERED_BY => undef, }; bless( $self, $class ); $event_count++ ; return $self; }
I have a sub class DeviceEventHandler which inherits the above EventHandler module.
I create object using DeviceEventHandler ->new() which will call the super class method.
Now my question is how do i add some sub class specific attributes say "DeviceReset" in DeviceEventHandler and instantiate it during object creation.
For that purpose I need to define a "new" method in DeviceEventHandler module which will in turn call the "new" method of the super class.

Please help me out on this..

Thanks.

Replies are listed 'Best First'.
Re: Inheritance- Sub class instance variables
by creamygoodness (Curate) on Aug 27, 2009 at 15:36 UTC

    package DeviceEventHandler; use base qw( EventHandler ); sub new { my $self = shift->SUPER::new(@_); $self->{DeviceReset} = undef; return $self; }

    You have to worry about parent and child classes stomping on each other's instance vars, but that's the basic idiom for hash-based objects.

Re: Inheritance- Sub class instance variables
by NetWallah (Canon) on Aug 27, 2009 at 18:15 UTC
    Hopefully, you have noticed by now that "Package EventHandler;" is incorrect - it should have a lower-case "p" for "package".

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)