我一直试图想出这个问题,直到今晚.我已经用Google搜索了它,并没有任何示例或我的黑客实例完成它.看起来这应该很简单,但我无法得到它.这是代码:
#!/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输出相同,这正是我想要实现的.
我在这里错过了什么?我应该“做”不同的做法,还是应该“反对”,如果是这样的话?
谢谢你的帮助…
解决方法
我们都有那些晚上!尝试:
$complex_variable = do $MEMORY || die "Bad data";