使用数据::自卸车Perl的持久性数据存储
-
22-09-2019 - |
题
我一直在想这一点的方式计算今晚长。我已经是一派死亡和无的例子或我的例子黑客都得非常好。看起来这应该是很容易的,但我不能得到它。下面是代码:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $complex_variable = {};
my $MEMORY = "$ENV{HOME}/data/memory-file";
$complex_variable->{ 'key' } = 'value';
$complex_variable->{ 'key1' } = 'value1';
$complex_variable->{ 'key2' } = 'value2';
$complex_variable->{ 'key3' } = 'value3';
print Dumper($complex_variable)."TEST001\n";
open M, ">$MEMORY" or die;
print M Data::Dumper->Dump([$complex_variable], ['$complex_variable']);
close M;
$complex_variable = {};
print Dumper($complex_variable)."TEST002\n";
# Then later to restore the value, it's simply:
do $MEMORY;
#eval $MEMORY;
print Dumper($complex_variable)."TEST003\n";
这是我的输出:
$VAR1 = {
'key2' => 'value2',
'key1' => 'value1',
'key3' => 'value3',
'key' => 'value'
};
TEST001
$VAR1 = {};
TEST002
$VAR1 = {};
TEST003
一切,我看说,TEST003输出应该等同于TEST001输出,这正是我想要的目的。
我缺少的是在这里吗?我应该是“做”荷兰国际集团不同的,或者我应该是“EVAL”荷兰国际集团,而不是如果又如何?
感谢所有帮助...
解决方案
我们都有那些日子啊!尝试:
$complex_variable = do $MEMORY || die "Bad data";
不隶属于 StackOverflow