Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3015972
  • 博文数量: 535
  • 博客积分: 15788
  • 博客等级: 上将
  • 技术积分: 6507
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 09:11
文章分类

全部博文(535)

文章存档

2016年(1)

2015年(1)

2014年(10)

2013年(26)

2012年(43)

2011年(86)

2010年(76)

2009年(136)

2008年(97)

2007年(59)

分类: Python/Ruby

2011-08-09 10:10:10

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语法的方式返回这个数据的内容。
  1. #!/usr/bin/perl -w
  2. use Data::Dumper;
  3. use Storable;

  4. my $a = "good";
  5. my @myarray = ("hello", "world", "123", 4.5);
  6. my %myhash = ( "foo" => 35,
  7.                 "bar" => 12.4,
  8.                  "2.5"=> "hello",
  9.                 "wilma" => 1.72e30,
  10.                 "betty" => "bye/n");

  11. print Dumper($a) ."\n"x2;

  12. print Dumper(\@myarray) ."\n"x2;

  13. print Dumper(\%myhash) ."\n"x2;

  14. print Dumper((\%myhash, \@myarray)) ."\n"x2;
运行结果如下:
  1. $VAR1 = 'good';


  2. $VAR1 = [
  3.           'hello',
  4.           'world',
  5.           '123',
  6.           '4.5'
  7.         ];


  8. $VAR1 = {
  9.           'betty' => 'bye/n',
  10.           'bar' => '12.4',
  11.           'wilma' => '1.72e+30',
  12.           'foo' => 35,
  13.           '2.5' => 'hello'
  14.         };


  15. $VAR1 = {
  16.           'betty' => 'bye/n',
  17.           'bar' => '12.4',
  18.           'wilma' => '1.72e+30',
  19.           'foo' => 35,
  20.           '2.5' => 'hello'
  21.         };
  22. $VAR2 = [
  23.           'hello',
  24.           'world',
  25.           '123',
  26.           '4.5'
  27.         ];
2、Storable
结合Data::Dumper和Storable存储和重新获取数据。你可以用U盘将数据拷走,再在其他服务器上展开。
  1. #!/usr/bin/perl -w
  2. use Data::Dumper;
  3. use Storable;

  4. my $a = "good";
  5. my @myarray = ("hello", "world", "123", 4.5);
  6. my %myhash = ( "foo" => 35,
  7.                 "bar" => 12.4,
  8.                  "2.5"=> "hello",
  9.                 "wilma" => 1.72e30,
  10.                 "betty" => "bye/n");

  11. print Dumper($a) ."\n"x2;

  12. print Dumper(\@myarray) ."\n"x2;

  13. print Dumper(\%myhash) ."\n"x2;

  14. print Dumper((\%myhash, \@myarray)) ."\n"x2;

  15. ###use Storable
  16. print "\nmethod 1,use Storable retrieve data:\n";
  17. store \%myhash,'./file.txt'; #保存数据
  18. my $hashref=retrieve('./file.txt'); #重新获取数据

  19. print Dumper(\%$hashref);






阅读(2157) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

tempo82011-08-10 21:29:34

谢谢分享!久旱逢甘霖!