zoukankan      html  css  js  c++  java
  • 第十一章 模块:

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

  • 相关阅读:
    【高级内部资料】.NET数据批量写入性能分析 第二篇
    负载均衡原理与实践详解 第五篇 负载均衡时数据包流程详解
    负载均衡原理与实践详解 第三篇 服务器负载均衡的基本概念网络基础
    如何提高Linq查询的性能(上)
    【全面解析DeepZoom 之二】Silverlight2及Deep Zoom环境的搭建
    关于让WPF软件界面支持全球化和本地化
    在WPF中自定义控件(3) CustomControl (上)
    【全面解析DeepZoom 之一】酷!Deep Zoom
    谈谈我理解的WPF团队模型——在UI Designer与Developer之间
    [WPF疑难]在WPF中显示动态GIF
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6198964.html
Copyright © 2011-2022 走看看