• 热门专题

关于Data::Dumper和Storable

作者:Xiajc工作笔记   发布日期:2011-08-09 18:22:42
Tag标签:Data::Dumper  Storable  
  • 1、Data::Dumper

    Another way to visualize a complex data structure rapidly is to dump it. A particularly nice dumping package is included in the Perl core distribution, called Data::Dumper.(intermediate perl chapter6)

    给定一个标量、数组、哈希或引用作为参数,将以PERL语法的方式返回这个数据的内容。

     

    #!/usr/bin/perl -w
    
    use Data::Dumper;
    
    use Storable;
    
    
    
    my $a = "good";
    
    my @myarray = ("hello", "world", "123", 4.5);
    
    my %myhash = ( "foo" => 35,
    
                    "bar" => 12.4,
    
                     "2.5"=> "hello",
    
                    "wilma" => 1.72e30,
    
                    "betty" => "bye/n");
    
    
    
    print Dumper($a) ."\n"x2;
    
    
    
    print Dumper(\@myarray) ."\n"x2;
    
    
    
    print Dumper(\%myhash) ."\n"x2;
    
    
    
    print Dumper((\%myhash, \@myarray)) ."\n"x2;
    
    

    运行结果如下:
     

    $VAR1 = 'good';
    
    
    
    
    $VAR1 = [
              'hello',
              'world',
              '123',
              '4.5'
            ];
    
    
    
    
    $VAR1 = {
              'betty' => 'bye/n',
              'bar' => '12.4',
              'wilma' => '1.72e+30',
              'foo' => 35,
              '2.5' => 'hello'
            };
    
    
    
    
    $VAR1 = {
              'betty' => 'bye/n',
              'bar' => '12.4',
              'wilma' => '1.72e+30',
              'foo' => 35,
              '2.5' => 'hello'
            };
    $VAR2 = [
              'hello',
              'world',
              '123',
              '4.5'
            ];
    
    

    2、Storable
    结合Data::Dumper和Storable存储和重新获取数据。你可以用U盘将数据拷走,再在其他服务器上展开。
     

    #!/usr/bin/perl -w
    
    use Data::Dumper;
    
    use Storable;
    
    
    
    my $a = "good";
    
    my @myarray = ("hello", "world", "123", 4.5);
    
    my %myhash = ( "foo" => 35,
    
                    "bar" => 12.4,
    
                     "2.5"=> "hello",
    
                    "wilma" => 1.72e30,
    
                    "betty" => "bye/n");
    
    
    
    print Dumper($a) ."\n"x2;
    
    
    
    print Dumper(\@myarray) ."\n"x2;
    
    
    
    print Dumper(\%myhash) ."\n"x2;
    
    
    
    print Dumper((\%myhash, \@myarray)) ."\n"x2;
    
    
    
    ###use Storable
    
    print "\nmethod 1,use Storable retrieve data:\n";
    
    store \%myhash,'./file.txt'; 			#保存数据
    
    my $hashref=retrieve('./file.txt');		#重新获取数据
    
    
    
    print Dumper(\%$hashref);
    

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规