+ -
当前位置:首页 → 问答吧 → perl oo编程如何调用方法?

perl oo编程如何调用方法?

时间:2010-08-02

来源:互联网

oo.pl
#!/usr/bin/perl -w
use strict;
use time2;
main()
{
my $time=new time2("statime";
print $time->getstatime();
}
&main();

-------------------------------------------------------------

time2.pm
#!/usr/bin/perl -w
use strict;
package time2;
sub new()
{
my $class=shift;
my $self={
statime=>undef,
notime=>undef};
bless $self,$class;
return $self;
}

sub getstatime()
{
my $self=shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
$sec=($sec<10)?"0$sec"sec;
$min=($min<10)?"0$min"min;
$hour=($hour<10)?"0$hour"hour;
$mday=($mday<10)?"0$mday"mday;
$mon=($mon<9)?"0".($mon+1)$mon+1);
$year+=1900;
$self->{"statime"}="[$year-$mon-$mday $hourminsec]";
return $self->{statime};
}

sub getnontime()
{
my $self=shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
$sec=($sec<10)?"0$sec"sec;
$min=($min<10)?"0$min"min;
$hour=($hour<10)?"0$hour"hour;
$mday=($mday<10)?"0$mday"mday;
$mon=($mon<9)?"0".($mon+1)$mon+1);
$year+=1900;
$self->{"notime"}="[$year-$mon-$mday\_$hour\_$min\_$sec]";
return $self->{notime};
}

[root@back liuqing]# ./oo.pl
time2.pm did not return a true value at ./oo.pl line 3.
BEGIN failed--compilation aborted at ./oo.pl line 3.

刚学perl,不知道该如何调用time2.pm(time2这个类写的应该有问题)里面的getstatime()的方法?望大牛们帮帮忙!

作者: leoxqing   发布时间: 2010-08-02

use package

作者: Wayne_sama   发布时间: 2010-08-02

热门下载

更多