如何将HASH 存入文件 然后将文件转为hash

perl Hash 我比较喜欢的,好多数据我都是存在 hash中。

但是有些数据我是不想他,程序退出后不保存的。所以想把数据存入文件中。

这个东西研究了 好多个小时,想找一个和 SHELL 语言一样的 >> << 文件读写方式。

 

经过 google 很一下。发现有几个方案是比较好的。

 

方案一Dump :

#!/bin/perl -w
use strict;
use Data::Dump 'dump';
use FileHandle;
my $file = "data.out";

{
 my %hash = ("apples"=>17,
    "bananas"=>9,
    "oranges"=>"none");
 my $str = Data::Dumper->Dump([ /%hash ], [ '$hashref' ]);
 my $out = new FileHandle ">$file";
 print $out $str;
 close $out;
}

{
 my $in = new FileHandle "<$file";
 local($/) = "";
 my $str  = <$in>;
 close $in;
 #print "Input: $str";
 my($hashref);
 eval $str;
 my %hash = %$hashref;
 print dump(/%hash),"/n";
}

 

 

方案2:使用Storable

 

#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Storable;
my %test = (this => ['that', 'the', 'other','323232'],
   that => ['this', 'the', 'other','121122']
   );
warn Dumper(/%test);
# Save the hash to a file:
store /%test, 'file.txt';
# Retrieve the hash from the file.
my $testref1 = retrieve('file.txt');
warn Dumper($testref1);

 

方案3:XML ()

没有细细研究

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值