Ciò avviene perchè non dici a DBM::Deep che quella che vuoi costruire è una struttura composta da un hash di hash.
prova ad eseguire da terminale il tuo codice con l'aggiunta seguente:
vedrai che la struttura che hai creato è la seguente:codice:use strict; use warnings; use diagnostics; use CGI::Carp "fatalsToBrowser"; use Data::Dumper; use DBM::Deep; my $db = new DBM::Deep "data.db"; my $myhash = "Zlatan"; $db->{$myhash}->{'name'} = "Zlatan"; $db->{$myhash}->{'age'} = "22"; print "Content-type: text/html","\n\n"; print "ok!\n"; print Dumper($db); exit(0);
come vedi ha incamerato solo il secondo elemento 'age' sovrascrivendo 'name'.codice:$VAR1 = bless( { 'Zlatan' => bless( { 'age' => '22' }, 'DBM::Deep' ) }, 'DBM::Deep' );
ora prova ad usare questo codice:
vedrai che la struttura sarà quella voluta:codice:use strict; use warnings; use diagnostics; use CGI::Carp "fatalsToBrowser"; use Data::Dumper; use DBM::Deep; my $db = new DBM::Deep "data.db"; my $myhash = "Zlatan"; $db->{$myhash} = {}; ## nota questo $db->{$myhash}->{'name'} = "Zlatan"; $db->{$myhash}->{'age'} = "22"; print "Content-type: text/html","\n\n"; print "ok!\n"; print Dumper($db); exit(0);
e che tutto funzionerà, grazie al fatto che hai indicato a DBM::Deep di costruire una struttura adatta.codice:$VAR1 = bless( { 'Zlatan' => bless( { 'age' => '22', 'name' => 'Zlatan' }, 'DBM::Deep' ) }, 'DBM::Deep' );

Rispondi quotando