先寫一個模組,以 package 為開始,定義一個 datetime 名稱,檔名存成 datetime.pm
# 起頭
package datetime;
將時間撈出來
my ($date_second,$date_minute,$date_hour,$date_day,$date_month,$date_year,$date_week)=localtime(time);
# 修正時間
$date_month++;
$date_year+=1900;if (length($date_second) == 1) {$date_second = “0”.”$date_second”;}
if (length($date_minute) == 1) {$date_minute = “0”.”$date_minute”;}
if (length($date_hour) == 1) {$date_hour = “0”.”$date_hour”;}
if (length($date_day) == 1) {$date_day = “0”.”$date_day”;}
if (length($date_month) == 1) {$date_month = “0”.”$date_month”;}# 將每個時間寫成一個 function,再回傳
sub second {
return $date_second;
}sub minute {
return $date_minute;
}sub hour {
return $date_hour;
}sub day {
return $date_day;
}sub month {
return $date_month;
}sub year {
return $date_year;
}sub week {
return $date_week;
}1;
怎麼使用, vi test.pl
use strict;
# 定義要使用 datetime 這個模組
use datetime;# 使用 datetime::你要的時間(也就是模組裡定義的 function 名稱)
my $sec = datetime::second;
my $min = datetime::minute;
my $hr = datetime::hour;
my $day = datetime::day;
my $month = datetime::month;
my $year = datetime::year;
my $week = datetime::week;
print “$sec,$min,$hr,$day,$month,$year,$week”;
留言