zoukankan      html  css  js  c++  java
  • 6、perl创建模块(Exporter)及路径 引用 嵌套 查询模块

    参考博客:http://www.cnblogs.com/xudongliang/tag/perl/

    1、perl 模块的创建以及制定perl 模块的路径

    (1)创建一个Myfun.pm模块。

    #/usr/bin/perl
    package Myfun;

    use warnings;

    use strict;

    sub sum($$){
    my ($a,$b)=@_;
    my $sum = $a+$b;
    return $sum;
    }
    1;

    (2)指定perl模块目录,运用Myfun.pm模块,

    比如 Myfun.pm 所在目录为/home/xudl/perl

    第一种方式: 设置环境变量PERL5LIB,

    在~/.bashrc 中添加      export PERL5LIB=$PERL5LIB:/home/xudl/perl;             source ~/.bashrc

    第二种方式, 在脚本中使用use lib

    use  lib "/home/xudl/perl";然后就可以使用   use Myfun ;

    
    

    #!/usr/bin/perl

    use  lib "/home/xudl/perl";

    use Myfun;

    my $aa= 12;my$bb=14;

    my $bb = Myfun::sum($a,$b);

    print qq{$bb };

    Exporter模块在创建模块时用到

    require Exporter;

    our @ISA =qw( Exporter);

    our @Export = qw(fun1 fun2)  #默认输出

    our @Export_OK = qw(fun3 fun4)  #要调用才能输出

    2、perl 中的引用

      使用 符号, 声明一个引用外,还可以声明一个匿名引用, 数组的引用使用 [  ] 操作符, 哈希的引用使用 {  } 操作符;

    (1)声明引用

    my @array     = (1, 2, 3);
    my $array_ref = @array;                   my $array_ref = [1, 2, 3, 4];
    my %hash     = (1, 2, 3, 4);
    my $hash_ref = \%hash;                     my $hash_ref  = {1, 2, 3, 4}

    (2)通过引用访问值
    print $array_ref->[0];    #数组的引用, 是->[ ] 中括号, 加上对应的下标.     匿名数组一样

    print $array_ref->{1}; #哈希的引用,通过->{} 大括号, 加上对应的key 匿名哈希一样
    (3)解引用  数组的引用通过 @ 符号 , 哈希的引用通过 % 符号

      my @array_new = @{$array_ref};
      my %hash_new = %{$hash_ref};


    3、perl 数组与散列的嵌套

    总公司下ABCDE各分公司人员
    数组的散列:@{$hash{A}}
    数组的散列的引用:@{$hash->{A}}
    总公司下ABCDE各分公司人员以及员工的收入
    散列的散列:%{$hash{A}}   
    散列的散列的引用:%{$hash->{A}}

    4、perl 模块查询

    模块路径:perl -V ;   perl -le 'print "@INC"'   或者   perl -e 'print join(" ",@INC)' 

    模块帮助:perldoc List::Util;

    某个模块安装路径:perldoc -l List::Util;

    查看所用在路径中的模块:find ·perl -le 'print "@INC"'· -name *.pm     

    模块安装与否:perl  -M List::Util -e ''




  • 相关阅读:
    5.18英语
    5.18
    5.17
    单源点最短路模板
    5.16
    mock.js进行接口mock
    docker-compose安装和使用
    docker常用命令
    docker安装和使用(win10家庭版)
    ES6基础(2)-const
  • 原文地址:https://www.cnblogs.com/renping/p/7398098.html
Copyright © 2011-2022 走看看