perl 自定义模块
Perl自定义模块
Timer.pm
package Hejw::Timer;
use Time::HiRes qw(sleep gettimeofday tv_interval);
#构造函数 sub new{ my $class = shift(); my $self = ();
$self->{"tm_begin"} = gettimeofday();
bless $self, $class;
return $self;
}
#方法 获取运行时间 sub get_interval{ my ($self) = @_; my $interval = gettimeofday() - $self->{"tm_begin"};
return sprintf("%.3f", $interval);
}
#方法 判断是否超时
sub is_time_out{
my($self, $time_out) = @_;
if (!$time_out){
print "time param is empty\n";
return 1;
}
if ($self->get_interval() > $time_out){
return 1;
}else{
return 0;
}
}
1;#必须要否则编译错误
myTimer.pl
#指定路径的两个方法 #方法一: use lib "./Hejw/"; #方法二: #BEGIN {
push (@INC,"./Hejw/");
#} #导出符号表 #require Timer; use Hejw::Timer;
#使用包模块 my $timer = Hejw::Timer->new(); sleep(2); print $timer->get_interval(), "\n"; print "is time out" if $timer->is_time_out(1);
作者:Jimmy1224 来源:CSDN 原文:https://blog.csdn.net/c_base_jin/article/details/78848465 版权声明:本文为博主原创文章,转载请附上博文链接!
http://www.runoob.com/perl/perl-packages-modules.html
require 引用需要使用包名指定函数,而 use 不需要,二者的主要区别在于:
1、require用于载入module或perl程序(.pm后缀可以省略,但.pl必须有) 2、Perl use语句是编译时引入的,require是运行时引入的 3、Perl use引入模块的同时,也引入了模块的子模块。而require则不能引入,要在重新声明 4、USE是在当前默认的@INC里面去寻找,一旦模块不在@INC中的话,用USE是不可以引入的,但是require可以指定路径 5、USE引用模块时,如果模块名称中包含::双冒号,该双冒号将作为路径分隔符,相当于Unix下的/或者Windows下的\