Help with Perl persistent data storage using Data::Dumper

Posted by stephenmm on Stack Overflow See other posts from Stack Overflow or by stephenmm
Published on 2010-03-26T04:25:50Z Indexed on 2010/03/26 4:33 UTC
Read the original article Hit count: 448

Filed under:
|
|

I have been trying to figure this out for way to long tonight. I have googled it to death and none of the examples or my hacks of the examples are getting it done. It seems like this should be pretty easy but I just cannot get it. Here is the code:

#!/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";   

And here is my output:

$VAR1 = {
         'key2' => 'value2',
         'key1' => 'value1',
         'key3' => 'value3',
         'key' => 'value'
       };
TEST001
$VAR1 = {};
TEST002
$VAR1 = {};
TEST003    

Everything that I read says that the TEST003 output should look identical to the TEST001 output which is exactly what I am trying to achieve.

What am I missing here? Should I be "do"ing differently or should I be "eval"ing instead and if so how?

Thanks for any help...

© Stack Overflow or respective owner

Related posts about perl

Related posts about persistent