Search notes:

Using Data::Dumper to store variables in a file and later retrieve them again

store_variable.pl

#!/usr/bin/perl
use warnings;
use strict;

use Data::Dumper;

my %test = (
  one => 1,
  two => 2,
  42  =>'forty two',
);

my $some_hash_ref = {
   foo => 'bar',
   now => 'then',
};

my $some_other_hash_ref = {
  some_hash_ref => $some_hash_ref
};

$some_hash_ref -> {some_other_hash_ref} = $some_other_hash_ref;

$test{some_hash_ref} = $some_hash_ref;

open  my $out, '>', 'variables.store' or die;
print $out Dumper(\%test);
close $out;
Github repository PerlModules, path: /Data/Dumper/store_variable.pl

retrieve_variable.pl

#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';

# use Data::Dumper;

open my $in, '<', 'variables.store' or die;
my @in = <$in>;
close $in;

# @in now contains an assignment to a variable:
print @in;
# $VAR1 = {
#           '42' => 'forty two',
#           'some_hash_ref' => {
#                                'some_other_hash_ref' => {
#                                                           'some_hash_ref' => $VAR1->{'some_hash_ref'}
#                                                         },
#                                'foo' => 'bar',
#                                'now' => 'then'
#                              },
#           'one' => 1,
#           'two' => 2
#         };


my $VAR1;

eval (join "", @in);
say "After first eval";

say $VAR1->{42};
# forty two

say $VAR1->{some_hash_ref}{foo};
# bar

say $VAR1->{some_hash_ref}{some_other_hash_ref}{some_hash_ref}{now};
# Use of uninitialized value …

print "\n\n";

eval (join "", @in);
say "After second eval";

say $VAR1->{some_hash_ref}{some_other_hash_ref}{some_hash_ref}{now};
# then
Github repository PerlModules, path: /Data/Dumper/retrieve_variable.pl

See also

Data::Dumper

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759415306, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/modules/store-retrieve-variables(122): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78