user2066945
user2066945

Reputation: 13

Save Perl hash permanently even script exit

I wanted to create a hash table that can save key and it's value permanently even when the scripts exit. Is that possible ?

for example:

For the first time I run this script , it take in $key and $value variable and store it into the hash table.

$key = 'perl';
$value = '123';
$hash{$key} = $value; 

for the second time when I run the same script, but I changed the value

$key = 'ruby';
$value = '456';
$hash{$key} = $value; 

and if i print out the hash table I will get perl with value 123 and ruby with value 456.

Upvotes: 1

Views: 426

Answers (3)

Patrick Wolf
Patrick Wolf

Reputation: 1319

While I agree Storable works, you can also use Data::Dumper which can provide a human readable /modifiable output. You can also read the data back into a Perl data structure.

Output

print $FH Data::Dumper->new([$hash_data], ['output123'])->Purity(1)->Dump;

Input

my $data_structure = do { local $/; <$FH> };
eval $data_structure;

One $data_structure is eval'd you will have a hashref @ $output123. $output is named by the parameter set when you initially did the dump.

Upvotes: 2

user554546
user554546

Reputation:

I don't have a lot of experience using it, but you can use Storable:

use strict;
use warnings;
use Storable;
store \%hash, 'file';

Alternatively, you can use a database or key-value store (MySQL, PostgreSQL, MongoDB, Couchbase, Riak, etc).

Upvotes: 3

atk
atk

Reputation: 9314

Write it to a file.

(extra characters to get to the 30char min post len)

Upvotes: 1

Related Questions