mr.nick has asked for the wisdom of the Perl Monks concerning the following question:
My first question is: what's the purpose ofuse strict; use warnings; package Event; sub new { my ($class) = @_; my $self = {}; $self = { 'Event Type' => 'Windows 2003', }; bless $self, ref($class) || $class; return $self; } package Security; our @ISA = qw (Event); @PARENT::ISA = @ISA; sub new { my $class = shift; my($self) = $class->PARENT::new(@_); $self->{'EventLog'} = 'Security'; return (bless($self, $class)); + # return object } 1;
@ISA = qw(Event); @PARENT::ISA = @ISA? To me, that seems that it would make each class a subclass of the other. Why do that?
For my second issue I need to show you the rest of the code, AuditLogReview_Event08.pm:
use strict; use warnings; package Event08; sub new { my ($class) = @_; my $self = {}; $self = { 'Event Type' => 'Windows 2008', }; bless $self, ref($class) || $class; return $self; } package Security08; our @ISA = qw (Event08); @PARENT::ISA = @ISA; sub new { my $class = shift; my($self) = $class->PARENT::new(@_); $self->{'EventLog'} = 'Security'; return (bless($self, $class)); + # return object } 1;
And finally, the test script, AuditTest.pl:
#!/usr/bin/perl -w use strict; use warnings; use AuditLogReview_Event; # handles 2003 events use AuditLogReview_Event08; # handles 2008 events EventWindowsHandler('.evt'); EventWindowsHandler('.evtx'); sub EventWindowsHandler { my $oEvent; my $ext = shift; if ($ext eq '.evt') { $oEvent = Security->new(); } elsif ( $ext eq '.evtx') { $oEvent = Security08->new(); # can I call Security from the + AuditLogReview_Event08 file? } print "Processing $oEvent->{'Event Type'} $oEvent->{'EventLog'} ev +ent.\n"; }
Now, the thing is, as written when it runs it produces the following:
C:\Users\A209624\Desktop>perl AuditTest.pl Processing Windows 2008 Security event. Processing Windows 2008 Security event.
Putting some debugging in there, I see that the Security->new() call goes to the right place, but when it does it's
$class->PARENT::new(@_)it's hitting Event08 instead of Event. Why?
I was able to get the behavior I wanted by changing the my $self = $class->PARENT::new(@_) in each package to
my $self = Event->new(@_); my $self = Event08->new(@_);
But I'm still unsure why the first way didn't work correctly.
Thanks!
mr.nick ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Can you help me understand this OO notation/issue?
by choroba (Cardinal) on Sep 06, 2013 at 15:00 UTC | |
by mr.nick (Chaplain) on Sep 06, 2013 at 15:08 UTC | |
Re: Can you help me understand this OO notation/issue?
by kcott (Archbishop) on Sep 07, 2013 at 07:16 UTC |