O
O. Olson
Hi,
I am practicing a bit of Perl and I have hit on to a problem. I am
more into C++/Java - so I am not confident about my Perl lingo.
I have two classes MyTime and ScheduleTask - each in packages.
ScheduleTask has two fields that should reference MyTime objects.
My problem is that in the as_text() SubRoutine of ScheduleTask I
cannot seem to be able to get the as_textTime() SubRoutine of the
MyTime objects. It tells me "Can't call method "as_textTime" without a
package or object reference ... "
Following are pieces of my Code: (I hope I got everything here)
---------------------------------------------------------
main.pl
---------------------------------------------------------
use warnings;
use strict;
use MyTime;
use ScheduleTask;
use String::Scanf;
my $str = "04:00:00 to 04:59:00 Do_Something";
my $schedule_task = new ScheduleTask();
$schedule_task->parseTask($str);
print $schedule_task->as_text . "\n"; # Here's the problem !!!
---------------------------------------------------------
ScheduleTask.pm
---------------------------------------------------------
package ScheduleTask;
use warnings;
use strict;
use String::Scanf;
use MyTime;
sub new {
my $inv = shift;
my $class = ref( $inv ) || $inv;
die "The SubRoutine new() cannot be called with any arguments\n"
unless @_ ==0;
my $self;
# For a new object the fields would be undefined
$self = {
START_TIME => undef,
STOP_TIME => undef,
NAME => undef
};
return bless $self, $class;
}
# Takes a string of the form "%02d:%02d:%02d to %02d:%02d:%02d %s" and
extracts the Task
sub parseTask {
my $task = $_[0]; # Get the Task Object passed
my $line = $_[1]; # Get the string
print "Line = $line \n";
my ($start_time_str, $stop_time_str, $name_str) =
String::Scanf::sscanf("%s to %s %s", $line);
my $start_time = new MyTime()->parseTime($start_time_str);
my $stop_time = new MyTime()->parseTime($stop_time_str);
# Finally collect all the above data together
($task->{START_TIME}, $task->{STOP_TIME}, $task->{NAME}) =
($start_time, $stop_time, $name_str);
#print as_text() . "\n"; # This does not seem to work
}
sub as_text {
my $self = shift; # Get the object
my $start_time = $self->{START_TIME};
my $stop_time = $self->{STOP_TIME};
return $start_time->as_textTime . " to " . $stop_time-
}
1;
---------------------------------------------------------
MyTime.pm
---------------------------------------------------------
package MyTime;
use warnings;
use strict;
use String::Scanf;
sub new {
my $inv = shift;
my $class = ref( $inv ) || $inv;
die "Too many arguments\n" unless @_ <=3;
my $self;
if ( @_ == 0 ) {
my @time = localtime( );
$self = { HOUR => $time[ 2 ],
MINUTE => $time[ 1 ],
SECOND => $time[ 0 ] };
} else {
$self = { HOUR => $_[ 0 ],
MINUTE => $_[ 1 ] || 0,
SECOND => $_[ 2 ] || 0 };
}
return bless $self, $class;
}
# Takes a string of the form "%02d:%02d:%02d" and extracts the Time
sub parseTime {
my $time = $_[0]; # Get the Time object passed
my $line = $_[1]; # Get the string
print "Line = $line \n";
($time->{HOUR}, $time->{MINUTE}, $time->{SECOND}) =
String::Scanf::sscanf("%02d:%02d:%02d", $line);
print "Parsed Time:" . $time->as_text . "\n";
}
sub as_textTime {
my $self = shift;
return sprintf "%02d:%02d:%02d",
$self->{ HOUR }, $self->{ MINUTE }, $self-
}
1;
---------------------------------------------------------
I am sorry for this long post - but I needed to post sufficient code
to reproduce my problem.
Thanks a lot,
O.O.
I am practicing a bit of Perl and I have hit on to a problem. I am
more into C++/Java - so I am not confident about my Perl lingo.
I have two classes MyTime and ScheduleTask - each in packages.
ScheduleTask has two fields that should reference MyTime objects.
My problem is that in the as_text() SubRoutine of ScheduleTask I
cannot seem to be able to get the as_textTime() SubRoutine of the
MyTime objects. It tells me "Can't call method "as_textTime" without a
package or object reference ... "
Following are pieces of my Code: (I hope I got everything here)
---------------------------------------------------------
main.pl
---------------------------------------------------------
use warnings;
use strict;
use MyTime;
use ScheduleTask;
use String::Scanf;
my $str = "04:00:00 to 04:59:00 Do_Something";
my $schedule_task = new ScheduleTask();
$schedule_task->parseTask($str);
print $schedule_task->as_text . "\n"; # Here's the problem !!!
---------------------------------------------------------
ScheduleTask.pm
---------------------------------------------------------
package ScheduleTask;
use warnings;
use strict;
use String::Scanf;
use MyTime;
sub new {
my $inv = shift;
my $class = ref( $inv ) || $inv;
die "The SubRoutine new() cannot be called with any arguments\n"
unless @_ ==0;
my $self;
# For a new object the fields would be undefined
$self = {
START_TIME => undef,
STOP_TIME => undef,
NAME => undef
};
return bless $self, $class;
}
# Takes a string of the form "%02d:%02d:%02d to %02d:%02d:%02d %s" and
extracts the Task
sub parseTask {
my $task = $_[0]; # Get the Task Object passed
my $line = $_[1]; # Get the string
print "Line = $line \n";
my ($start_time_str, $stop_time_str, $name_str) =
String::Scanf::sscanf("%s to %s %s", $line);
my $start_time = new MyTime()->parseTime($start_time_str);
my $stop_time = new MyTime()->parseTime($stop_time_str);
# Finally collect all the above data together
($task->{START_TIME}, $task->{STOP_TIME}, $task->{NAME}) =
($start_time, $stop_time, $name_str);
#print as_text() . "\n"; # This does not seem to work
}
sub as_text {
my $self = shift; # Get the object
my $start_time = $self->{START_TIME};
my $stop_time = $self->{STOP_TIME};
return $start_time->as_textTime . " to " . $stop_time-
as_textTime . " " . $self->{NAME};
}
1;
---------------------------------------------------------
MyTime.pm
---------------------------------------------------------
package MyTime;
use warnings;
use strict;
use String::Scanf;
sub new {
my $inv = shift;
my $class = ref( $inv ) || $inv;
die "Too many arguments\n" unless @_ <=3;
my $self;
if ( @_ == 0 ) {
my @time = localtime( );
$self = { HOUR => $time[ 2 ],
MINUTE => $time[ 1 ],
SECOND => $time[ 0 ] };
} else {
$self = { HOUR => $_[ 0 ],
MINUTE => $_[ 1 ] || 0,
SECOND => $_[ 2 ] || 0 };
}
return bless $self, $class;
}
# Takes a string of the form "%02d:%02d:%02d" and extracts the Time
sub parseTime {
my $time = $_[0]; # Get the Time object passed
my $line = $_[1]; # Get the string
print "Line = $line \n";
($time->{HOUR}, $time->{MINUTE}, $time->{SECOND}) =
String::Scanf::sscanf("%02d:%02d:%02d", $line);
print "Parsed Time:" . $time->as_text . "\n";
}
sub as_textTime {
my $self = shift;
return sprintf "%02d:%02d:%02d",
$self->{ HOUR }, $self->{ MINUTE }, $self-
{ SECOND };
}
1;
---------------------------------------------------------
I am sorry for this long post - but I needed to post sufficient code
to reproduce my problem.
Thanks a lot,
O.O.