模块

11.1  使用模块:

因为模块使用 Exporter 把符号输入到当前包里,所以你可以不加包限制词地使用来自该
模块的符号:

因为模块使用 Exporter 把符号输入到当前包里,所以你可以不加包限制词地使用来自该
模块的符号:
[root@wx03 5]# cat Fred.pm 
package Fred;
use Exporter 'import';
@EXPORT = qw(flintstone);
sub flintstone{

return 99;
};
1;

[root@wx03 5]# cat a1.pl 
use Fred  qw/flintstone/;
print "first.........
";
print Fred::flintstone();
print "
";
print "second.........
";
print flintstone();
print "
";

[root@wx03 5]# perl a1.pl 
first.........
99
second.........
99

11.2  创建模块	


个模块可以有两个方法把它的接口提供给你的程序使用:

 把符号输出或者允许方法调用。


11.2.1 模块私有和输出器

[root@wx03 5]# cat Fred.pm 
package Fred;
use Exporter 'import';
@EXPORT = qw(flintstone $var);

sub flintstone{

return 99;
};
our $var="SADA1311";
1;
[root@wx03 5]# cat a1.pl 
use Fred  qw/flintstone $var/;
print "first.........
";
print Fred::flintstone();
print "
";
print "second.........
";
print flintstone();
print "
";
print "third..............
";
print $var;
print "
";

[root@wx03 5]# perl a1.pl 
first.........
99
second.........
99
third..............
SADA1311


利用EXPORT 导出函数和方法:


my 局部变量无法导出:

[root@wx03 5]# cat Fred.pm 
package Fred;
use Exporter 'import';
@EXPORT = qw(flintstone $var);

sub flintstone{

return 99;
};
my $var="SADA1311";
1;
[root@wx03 5]# perl a1.pl 
first.........
99
second.........
99
third..............


11.2 创建模块:

一个模块可以有两个方法把它的接口提供给你的程序使用:把符号输出或者
允许方法调用。我们在这里先给你演示一个第一种风格的例子;第二种风格用于面向对象的
模块,我们将在下一章里描述。


面向过程就是把符号输出 比如函数变量等


面向对象就是提供方法调用



package Bestiary;
require Exporter;

##继承(Exporter)类
our @ISA =qw(Exporter);
our @EXPORT =qw(camel); # 缺省输出的符号
our @EXPORT_OK =qw($weight); # 按要求输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
~                                                                                                                      

一个程序现在可以说 use Bestiary 就能访问 camel 函数(但是不能访问 $weight 变
量),或者 use Bestiary qw(camel, $weight) 可以访问函数和变量。

[tomcat@wx03 ~]$ cat a5.pl 
use Bestiary qw(camel $weight);
&camel;
print "
";
print $weight ;
print "
";
[tomcat@wx03 ~]$ cat Bestiary.pm 
package Bestiary;
use Exporter 'import';
##
our @EXPORT =qw(camel $weight); # 缺省输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
[tomcat@wx03 ~]$ cat a5.pl 
use Bestiary qw(camel $weight);
&camel;
print "
";
print $weight ;
print "
";
[tomcat@wx03 ~]$ perl a5.pl 
One-hump dromedary
1024


11.2.1 模块私有和输出器:

require Exporter;
our @ISA = ("Exporter");

这两行令该模块从 Exporter 类中继承下来。我们在下一章讲继承,但在这里你要知道的
所有东西就是我们的 Bestiary 模块现在可以用类似下面的行把符号输出到其他包里:



@EXPORT 数组包含缺省时要输出的变量和函数的名字:当你的
程序说 use Bestary 的时候得到的东西。


在 @EXPORT_OK 里的变量和函数只有当程序
在 use 语句里面特别要求它们的时候才输出

@EXPORT:

[tomcat@wx03 ~]$ cat Bestiary.pm 
package Bestiary;
use Exporter 'import';
##
our @EXPORT =qw(camel $weight); # 缺省输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
[tomcat@wx03 ~]$ perl a5.pl 
One-hump dromedary
1024[tomcat@wx03 ~]$ 


@EXPORT_OK :

1024[tomcat@wx03 ~]$ cat Bestiary.pm 
package Bestiary;
use Exporter 'import';
##
our @EXPORT_OK =qw(camel $weight); # 缺省输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
[tomcat@wx03 ~]$ cat a5.pl 
use Bestiary qw(camel $weight);
&camel;
print "
";
print $weight ;
[tomcat@wx03 ~]$ perl a5.pl 
One-hump dromedary
1024[tomcat@wx03 ~]$ 


use Bestiary; # 输入@EXPORT 符号
use Bestiary(); # 什么也不输入
use Bestiary qw(ram @llama); # 输入ram 函数 和@llama 数组




原文地址:https://www.cnblogs.com/hzcya1995/p/13350746.html