perl写模块和用模块例子
时间:2007-12-18 来源:tonywam1036
从perl5 unleashed上抄下来的
#首先写一个模块 Letter.pm
package Letter;
require Exporter;
@ISA = (Exporter); @EXPORT = qw(Date
To
ClaimMoney
ClaimMoneyNice
ThankDem
Finish); #------------------------------------------------------- #上面三条都是样板戏了 sub Letter::Date {
$date = `date`;
print "\n Today is $date";
} sub Letter::To {
local($name) = shift;
local($subject) = shift;
print "\n To: $name";
print "\n Fm: Rudious Maximus, Loan Shark";
print "\n Dt: ", `date`;
print "\n Re: $subject";
print "\n\n";
print "\n====================================================\n";
}
sub Letter::ClaimMoney() {
print "\n You owe me money. Get your act together";
print "\n Do you want me to send Bruno over to ";
print "\n collect it , or are you gonna pay up?";
} sub Letter::ClaimMoneyNice() {
print "\n It is come to my attention that your account is ";
print "\n way over due.";
print "\n You gonna pay us soon..";
print "\n or would you like me to come ovah?";
} sub Letter::ThreatBreakLeg() {
print "\n apparently letters like these dont help";
print "\n I will have to make an example of you";
print "\n \n See you in the hospital, pal!";
} sub Letter::ThankDem() {
print "\n\n Thanks for your support";
} sub Letter::Finish(){
printf "\n\n\n\n Sincerely";
printf "\n Rudious \n ";
} 1; #最后的 1 是必须的,规定如此
#--------------------------------------------------------------------------- #上面的内容保存为Letter.pm文件 这里用模块了 #!/usr/bin/perl -w
use Letter; Letter::To("Mr. Gambling Man","The money for Lucky Dog, Race 2");
Letter::ClaimMoneyNice();
Letter::ThankDem();
Letter::Finish(); date命令在windows上好像有小小问题,linux下可以。
@ISA = (Exporter); @EXPORT = qw(Date
To
ClaimMoney
ClaimMoneyNice
ThankDem
Finish); #------------------------------------------------------- #上面三条都是样板戏了 sub Letter::Date {
$date = `date`;
print "\n Today is $date";
} sub Letter::To {
local($name) = shift;
local($subject) = shift;
print "\n To: $name";
print "\n Fm: Rudious Maximus, Loan Shark";
print "\n Dt: ", `date`;
print "\n Re: $subject";
print "\n\n";
print "\n====================================================\n";
}
sub Letter::ClaimMoney() {
print "\n You owe me money. Get your act together";
print "\n Do you want me to send Bruno over to ";
print "\n collect it , or are you gonna pay up?";
} sub Letter::ClaimMoneyNice() {
print "\n It is come to my attention that your account is ";
print "\n way over due.";
print "\n You gonna pay us soon..";
print "\n or would you like me to come ovah?";
} sub Letter::ThreatBreakLeg() {
print "\n apparently letters like these dont help";
print "\n I will have to make an example of you";
print "\n \n See you in the hospital, pal!";
} sub Letter::ThankDem() {
print "\n\n Thanks for your support";
} sub Letter::Finish(){
printf "\n\n\n\n Sincerely";
printf "\n Rudious \n ";
} 1; #最后的 1 是必须的,规定如此
#--------------------------------------------------------------------------- #上面的内容保存为Letter.pm文件 这里用模块了 #!/usr/bin/perl -w
use Letter; Letter::To("Mr. Gambling Man","The money for Lucky Dog, Race 2");
Letter::ClaimMoneyNice();
Letter::ThankDem();
Letter::Finish(); date命令在windows上好像有小小问题,linux下可以。
相关阅读 更多 +