Reputation: 145
I'm still in the process of learning the basic fundamentals of OO Perl. I have created a package called BigBrother.pm with a basic constructor.
package BigBrother;
use strict;
use warnings;
sub new {
my ($class, $args) = @_;
my $self = {
datacenter => $args->{datacenter},
testname => $args->{testname},
hostname => $args->{hostname},
ipaddress => $args->{ipaddress},
eventtime => $args->{eventtime},
responsetime => $args->{responsetime},
error => $args->{error} || ''
};
return bless $self, $class;
}
I also created two methods: one that records the time before the event started and the other to record the time when the event stopped.
sub setTimer {
my $self = shift;
my $start = `date +%s.%N`;
$self->{startTimer} = $start->startTimer;
}
sub endTimer {
my $self = shift;
my $end = `date +%s.%N`;
return $self->{endTimer} = $end->endTimer;
}
1;
In my perl executable script, I have the following:
#!/usr/bin/perl -w
use warnings;
use strict;
use Xymon::Plugin::Server::Hosts;
use lib "/usr/lib/xymon/server/ext";
use BigBrother;
BEGIN {
$ENV{LD_LIBRARY_PATH}= "/usr/lib/xymon/server/ext/";
}
my $bigbro = new BigBrother;
my $ret = $bigbro->setTimer;
But I'm getting the following error when I run the script:
# ./a.pl
Can't locate object method "startTimer" via package "1422368625.492495754
" (perhaps you forgot to load "1422368625.492495754
"?) at /usr/lib/xymon/server/ext/BigBrother.pm line 22.
This is line 22 of BigBrother.pm
sub setTimer {
20 my $self = shift;
21 my $start = `date +%s.%N`;
22 $self->{startTimer} = $start->startTimer;
23 }
24
25 sub endTimer {
26 my $self = shift;
27 my $end = `date +%s.%N`;
28 $self->{endTimer} = $end->endTimer;
29 }
Obviously, I'm missing something. Can someone please clarify?
Upvotes: 1
Views: 401
Reputation: 91430
my $start = `date +%s.%N`;
The above stores a string (not an object) in $start
. Change your sub to:
sub setTimer {
my $self = shift;
my $start = `date +%s.%N`;
$self->{startTimer} = $start;
}
sub endTimer {
my $self = shift;
my $end = `date +%s.%N`;
return $self->{endTimer} = $end;
}
Upvotes: 1
Reputation: 15391
These are the two relevant lines:
21 my $start = `date +%s.%N`;
22 $self->{startTimer} = $start->startTimer;
You call date
and store its output as string in scalar variable $start
. Then you try to run the method startTimer
on this non-object. This doesn't work out what is perfectly correct.
Do you have defined a startTimer
method somewhere or do you some module that should provide it?
Upvotes: 0