Perl Data:Dumper和Storable的用法

1、Data::Dumper
给定一个标量、数组、哈希或引用作为参数,将以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);


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值