第十一章 模块:
传统模块为调用者的输入和使用定义了子过程和 变量。面向对象的模块的
运转类似类声明并且是通过方法调用来访问的
如果你的模块的名字是 Red::Blue::Green,Perl 就会把它看作Red/Blue/Green.pm。
11.2 创建模块
我们前面说过,一个模块可以有两个方法把它的接口提供给你的程序使用:符号输出或者允许方法调用
面向对象的模块应该不输出任何东西,因为方法最重要的改变就是Perl以该对象的类型为基础自动帮你找到方法的自身
/********* 第一种使用@EXPORT 来导出符号
Vsftp:/root/perl/7# cat Bestiary.pm
package Bestiary;
require Exporter;
our @ISA =qw(Exporter);
our @EXPORT =qw($weight camel); # 按要求输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
Vsftp:/root/perl/7# cat a10.pl
unshift(@INC,"/root/perl/7");
use Bestiary ;
print "\$weight is $weight\n";
my $var=camel ();
print "\$var is $var\n";
Vsftp:/root/perl/7# perl a10.pl
$weight is 1024
One-hump dromedary$var is 1
/*******************第2种使用@EXPORT_OK
Vsftp:/root/perl/7# cat Bestiary.pm
package Bestiary;
require Exporter;
our @ISA =qw(Exporter);
our @EXPORT_OK =qw($weight camel); # 按要求输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
Vsftp:/root/perl/7# cat a10.pl
unshift(@INC,"/root/perl/7");
use Bestiary ;
print "\$weight is $weight\n";
my $var=camel ();
print "\$var is $var\n";
Vsftp:/root/perl/7# perl a10.pl
$weight is
Undefined subroutine &main::camel called at a10.pl line 5.
此时无法调用,需要use Bestiary qw($weight camel) ;
Vsftp:/root/perl/7# cat a10.pl
unshift(@INC,"/root/perl/7");
use Bestiary qw($weight camel) ;
print "\$weight is $weight\n";
my $var=camel ();
print "\$var is $var\n";
Vsftp:/root/perl/7# perl a10.pl
$weight is 1024
One-hump dromedary$var is 1
11.2.1 模块私有和输出器
require Exporter;
our @ISA = ("Exporter");
这两行命令该模块从Exporter 类中继承下来,我们在下一章讲继承,
但是在这里你要知道的,所有东西就是我们的Bestiary 模块现在可以用
类似下面的行把符号表输出到其他包里:
从输出模块的角度出发,@EXPORT 数组包含缺省要输出的变量和函数的名字: 当你的程序说
use Bestary 的时候得到的东西,在@EXPORT_OK里的变量和函数 只有当程序在use 语句里面
特别要求它们的时候才输出。
Vsftp:/root/perl/7# cat Bestiary.pm
package Bestiary;
require Exporter;
our @ISA =qw(Exporter);
our @EXPORT_OK =qw($weight camel); # 按要求输出的符号
our $VERSION = 1.00; # 版本号
### 在这里包含你的变量和函数
sub camel { print "One-hump dromedary" }
$weight = 1024;
1;
Vsftp:/root/perl/7# cat a10.pl
unshift(@INC,"/root/perl/7");
#use Bestiary qw($weight camel) ;
BEGIN {
require Bestiary;
import Bestiary qw($weight camel) ;
}
print "\$weight is $weight\n";
my $var=camel ();
print "\$var is $var\n";
Vsftp:/root/perl/7# perl a1
a10.pl a1.pl
Vsftp:/root/perl/7# perl a10.pl
$weight is 1024
One-hump dromedary$var is 1
第十一章 模块:
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
小满nestjs(第十一章 nestjs 模块)
每个 Nest 应用程序至少有一个模块,即根模块。事实上,根模块可能是应用程序数。
java 开发语言 应用程序 ide App -
第十一章 图像
介绍 图像模块提供了当前设备和应用窗口的信息和访问OpenGL上下文的信息。具体来说,有关屏幕大小,像素密度和帧缓冲特性比如颜色深度,抗锯齿等。与其他模块一样,通常通过Gdx类进行访问。 Open...
javascript android游戏开发 腾讯微博 游戏开发框架 抗锯齿