Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Inheritance- Sub class instance variables

by snra_perl (Acolyte)
on Aug 27, 2009 at 15:32 UTC ( [id://791683]=perlquestion: print w/replies, xml ) Need Help??

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://791683]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found