use warnings;
use strict;
use JSON;
my %first_hash = (fruit => 'banana', Vegetable => 'tomato');
my %second_hash = (work => 'office', family => 'home');
my %hashes = (
first_hash => \%first_hash,
second_hash => \%second_hash,
);
my $json = encode_json \%hashes;
open my $fh, '>', 'data.json' or die $!;
print $fh $json;
####
{"second_hash":{"work":"office","family":"home"},"first_hash":{"Vegetable":"tomato","fruit":"banana"}}
####
use warnings;
use strict;
use Data::Dumper;
use JSON;
if (! @ARGV && ($ARGV[0] ne 'first_hash' || $ARGV[0] ne 'second_hash')){
print "need first_hash or second_hash as arg\n";
exit;
}
my $want = $ARGV[0];
my $file = 'data.json';
my $json;
{
local $/;
open my $fh, '<', $file or die $!;
$json = <$fh>;
close $fh;
}
my $data = decode_json $json;
print Dumper $data->{$want};
####
$ perl fetch_hash.pl first_hash
$VAR1 = {
'fruit' => 'banana',
'Vegetable' => 'tomato'
};
$ perl fetch_hash.pl second_hash
$VAR1 = {
'work' => 'office',
'family' => 'home'
};