• 第十一章 模块:


    第十一章 模块:
    
    传统模块为调用者的输入和使用定义了子过程和 变量。面向对象的模块的
    运转类似类声明并且是通过方法调用来访问的
    
    
    如果你的模块的名字是 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
    ";
    
    my $var=camel ();
    print "$var is $var
    ";
    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
    ";
    
    my $var=camel ();
    print "$var is $var
    ";
    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
    ";
    
    my $var=camel ();
    print "$var is $var
    ";
    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
    ";
    
    my $var=camel ();
    print "$var is $var
    ";
    
    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

  • 相关阅读:
    苹果将首次采用HTML5直播发布会 狼人:
    Python 3.2 alpha 2发布 狼人:
    下一代Linux文件系统Btrfs走向成熟 狼人:
    Hello! 404 狼人:
    退格回车控制台输入密码
    poj 3233 Matrix Power Series
    地址参考clang: error: linker command failed with exit code 1 (use v to see invocation)
    文本截断JQuery为textarea添加maxlength,并且兼容IE
    代码下载Html5初探视频元素video示例
    c# 限制textbox的输入范围和长度(长度不用maxlength方法)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350009.html
Copyright © 2020-2023  润新知