Perl中的文件包含(do, require, use) | 如何编写Perl模块

Perl中的文件包含,有3中方法:do, require, 以及use

1. do:

1)形式:
do 'filename';
说明:
这里filename需要添加单引号,否则会出错;
filename可以为任何后缀的,甚至没有后缀,不要求是pl或者pm等。

2)关于do的理解:
do 'filename'
首先需要读入filename的文件(如果读入失败,返回undef而且会设置$!变量);
如果读入成功,然后对filename读入的语句进行编译(如果无法编译或者编译错误,会返回undef而且设置错误信息到$@变量);
如果编译也成功,do会执行filename中的语句,最终返回最有一个表达式的值。

简短表达do 'filename'的功能,就是能够将filename中的文字全部加载到当前文件中。

3)理解do的用法:
a. 将文件拆分:
main.pl:

use strict;

do ' seperate ' ; # 文件可以以任何后缀命名甚至没有后缀;


seperate:
print " Hellofromseperatefile!:-) " ;


b. 可以在seperate中定义函数,然后在当前文件中调用:
main.pl

use strict;

do ' seperate ' ;
show();


seperate:

subshow{
print
" Hellofromseperatefile!:-) " ;
}


c. 可以在seperate中定义package,然后在当前文件中调用:
main.pl

use strict;

do ' seperate ' ;
Show
:: show_sentence();


seperate:
packageShow ;
subshow_sentence(){
print
" Hellofromseperatefile!:-) " ;
}
1 ;
__END__

#都不需要文件名必须与package的名称相同,而且seperate都不需要pm后缀。

--------------------------------------------
从上面的例子,很容易得到,使用do可以方便地实现文件包含。

更多参看http://perldoc.perl.org/functions/do.html

2. require
参看http://perldoc.perl.org/functions/require.html
1)形式:
require 'filename';
require "filename";
这两种相同,而且和do的使用方法都类似;

require Module;
如果不加单引号或者双引号的话,后面的Module将被解析为Perl的模块即.pm文件,然后根据@INC Array中搜索Module.pm文件。首先在当前目录下搜索Module.pm的文件(用户自定义的),如果找不到再去Perl的 (@INC contains: C:/Perl/site/lib C:/Perl/lib .)寻找。
如果Module中出现::,如require Foo::Bar; 则会被替换为Foo\Bar.pm

2)关于require使用的解释:
如果使用require 'filename'或者require "filename"来包含文件的话,使用方法和do完全近似;
如果使用require Module的话,则需要定义Module.pm的文件而且文件中的package要以Module来命名该模块。
main.pl

# !C:perlinperl-w
use strict;

require Show;

Show
:: show_header();

Show.pm
# Show.pm

package Show;

sub show_header(){
print " Thisistheheader! " ;
return 0 ;
}

sub show_footer(){
print " Thisisthefooter! " ;
return 0 ;
}

1 ;

__END__



3. use
参看http://perldoc.perl.org/functions/use.html
1)形式:
use Module;
use只能够使用模块,而且和require的用法相似,需要Module.pm的文件,而且文件中的package需要已Module来命名该模块。

main.pl
# !C:perlinperl-w
use strict;

use Show;

Show
:: show_header();

Show.pm
# Show.pm

package Show;

sub show_header(){
print " Thisistheheader! " ;
return 0 ;
}

sub show_footer(){
print " Thisisthefooter! " ;
return 0 ;
}

1 ;

__END__


2)require和use的区别:
require:
do the things at run time; (运行时加载)
use:
do the things at compile time; (编译时加载)

4. perlmod - Perl modules (packages)
参考http://perldoc.perl.org/perlmod.html
1) 示例:
#Show.pm

package Show;

sub show_header(){
print "This is the header! \n";
return 0;
}

sub show_footer(){
print "This is the footer! \n";
return 0;
}

1;

__END__

2)
一般文件名需要和package名称相同,这里为Show;
可以定义变量和函数;
不要忘记1;
以及最后的__END__

3)
在别的文件中,使用require或者use使用模块的时候:
use Show;
#require Show;

Show::show_header();

5. Perl的函数定义及调用:
sub fun_name(){
#...
}
1) 如果定义在使用之前,在使用的时候直接fun_name();即可
2)如果定义在使用之后,之前使用的时候需要使用&fun_name();来调用函数。

6.
小结:
综上,文件包含可以提高代码的复用性,在Perl是实现文件包含可以才去两条路:
1)使用do或者require(带引号的)那种方式;
2)使用require Module或者use Module的模块方式;
两者均可。


7.
#参考资料:
#http://www.thescripts.com/forum/thread652009.html
#require: http://perldoc.perl.org/functions/require.html
#use: http://perldoc.perl.org/functions/use.html
#do: http://perldoc.perl.org/functions/do.html

#Perl modules (packages and symbol tables):
#http://perldoc.perl.org/perlmod.html

#Advanced: http://search.cpan.org/%7Enwclark/perl-5.8.8/lib/Exporter.pm

#http://www.perlmonks.org/?node_id=533866
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值