zoukankan      html  css  js  c++  java
  • [Perl] Data::Dumper模块的用法简介

    Perl由于有了引用,使得我们可以在不同的数据结构之间灵活的嵌套数据结构。
    比方说,Hash的value可以是标量,也可以嵌套list,甚至还可以继续嵌套hash。

    这样使得我们写代码的确方便了不少,但有的时候我们希望对这些复杂的数据结构
    有个直观的感受,也就是说能够用perl的语法吧数据结构以及实际值表示出来。这在开发阶段尤其
    有用!

    正好Perl的模块Data::Dumper可以帮助我们干这件事。

    Data::Dumper有面向对象和直接使用函数两种调用方法,

    这里介绍直接使用函数的方式,简单好用,应该能够满足绝大多数需求:

    Dumper接收的参数为一个标量的列表或者一个引用的列表。
    my $a = "good";
    my $b = "bad";
    my @my_array = ("hello", "world", "123", 4.5);
    my %some_hash = ("foo", 35, "bar", 12.4, 2.5, "hello",
              "wilma", 1.72e30, "betty", "bye\n");

    ##使用函数
    print Dumper($a);
    print Dumper(\@my_array);
    print Dumper(\%some_hash);
    print Dumper((\%some_hash, \@my_array));

    运行效果:
    roger@roger-desktop:~/sandbox$ perl dump.pl
    $VAR1 = 'good';
    $VAR1 = [
              'hello',
              'world',
              '123',
              '4.5'
            ];
    $VAR1 = {
              'betty' => 'bye
    ',
              'bar' => '12.4',
              'wilma' => '1.72e+30',
              'foo' => 35,
              '2.5' => 'hello'
            };
    $VAR1 = {
              'betty' => 'bye
    ',
              'bar' => '12.4',
              'wilma' => '1.72e+30',
              'foo' => 35,
              '2.5' => 'hello'
            };
    $VAR2 = [
              'hello',
              'world',
              '123',
              '4.5'
            ];
    程序的输出会按照引用在list中的位置自动命名VAR[n].

        # if debug flag open, dump key parameters from Launcher
        if ( $debug ) {

            use Data::Dumper;

            $Data::Dumper::Sortkeys = 1; #Sort the keys in the output
            $Data::Dumper::Deepcopy = 1; #Enable deep copies of structures
            $Data::Dumper::Indent = 2;   #Output in a reasonable style (but no array indexes)

            $vmstaf->WriteTestLog( "DEBUG", "VMOTION : " .
                "Dumper Key Parameters from Launcher." );

            $vmstaf->WriteTestLog( "DEBUG", "VMOTION : " .
                "Dumper :: HASH :: generalInfo" );
            print Dumper(\%generalInfo);

            $vmstaf->WriteTestLog( "DEBUG", "VMOTION : " .
                "Dumper :: HASH :: vcInfo" );
            print Dumper(\%vcInfo);

            $vmstaf->WriteTestLog( "DEBUG", "VMOTION : " .
                "Dumper :: HASH :: vmNameToEsx" );
            print Dumper(\%vmNameToEsx);

            $vmstaf->WriteTestLog( "DEBUG", "VMOTION : " .
                "Dumper :: HASH :: esxToSwitches" );
            print Dumper(\%esxToSwitches);

            $vmstaf->WriteTestLog( "DEBUG", "VMOTION : " .
                "Dumper :: HASH :: esxToDevs" );
            print Dumper(\%esxToDevs);

        }

  • 相关阅读:
    大数据运维(40)ElasticSearch安装ik中文分词器
    大数据运维(39)Kylin 3.1.0集群部署
    大数据运维(38)Flink 1.11.1部署安装
    项目实战 从 0 到 1 学习之Flink(28)Flink 1.11 新特性:流批一体的 Hive 数仓
    项目实战从0到1之hive(29)企业级数据仓库构建(十):搭建 ADS 层
    项目实战从0到1之hive(28)数仓项目(九)数仓搭建-DWT 层
    项目实战从0到1之hive(27)数仓项目(九)数仓搭建
    项目实战从0到1之hive(26)企业级数据仓库构建(八):搭建DWD 层-业务数据
    项目实战从0到1之hive(25)企业级数据仓库构建(七):搭建DWD 层
    项目实战从0到1之hive(24)企业级数据仓库构建(六):数仓理论及数仓搭建
  • 原文地址:https://www.cnblogs.com/licheng/p/1612239.html
Copyright © 2011-2022 走看看