Persistent Perl data storage using Data :: Dumper

I'm trying to figure it out today to go a long way. I killed him to death, and none of the examples or my hacking examples did this. It seems to be pretty easy, but I just can't 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 simply:
do $MEMORY;
#eval $MEMORY;

print Dumper($complex_variable)."TEST003\n";   

And here is my conclusion:

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

All that I read suggests that the output of TEST003 should look exactly like the result of TEST001, and that is what I am trying to achieve.

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

Thanks for any help ...

+3
source share
3 answers

We all have such evenings! Try:

$complex_variable = do $MEMORY || die "Bad data";
+5

-, Storable.pm Data:: Dumper. Storable , .

-, , , , " $MEMORY". eval . :

$complex_variable = eval $MEMORY;
print Dumper($complex_variable)."TEST003\n";
+6

DBM:: Deep . , " " Perl", , .

+1

Source: https://habr.com/ru/post/1738609/


All Articles