Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    problemi modulo DBM::Deep

    Sto cercando di capire come funziona veramente il modulo DBM::Deep

    Con questo codice scrivo il DB:

    codice:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics;
    use CGI::Carp "fatalsToBrowser";
    
    use lib '.';
    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
    
    ";
    
    exit(0);
    e con questo lo leggo:

    codice:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics;
    use CGI::Carp "fatalsToBrowser";
    
    use lib '.';
    use DBM::Deep;
    
    my $db = new DBM::Deep "data.db";
    
    my $myhash = "Zlatan";
    
    my $nome = $db->{$myhash}->{name};
    my $eta = $db->{$myhash}->{age};
    
    print "Content-type: text/html","\n\n";
    print "ok!\n
    $nome
    $eta
    ";
    
    exit(0);
    quando eseguo la lettura ottengo i valori giusti: zlatan e 22

    ma se in quest'ultimo codice al posto di my $myhash = "Zlatan"; metto my $myhash = "marameo";

    mi continua a dare ancora: zlatan e 22

    ma perchè??????

    Qualcuno sa aiutarmi con questo modulo??

    Grazie...

  2. #2
    ciao, prima di inserire qualcosa prova a fare così:
    codice:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics;
    use CGI::Carp "fatalsToBrowser";
    
    use lib '.';
    use DBM::Deep;
    
    my $db = new DBM::Deep "data.db";
    
    my $myhash = "Zlatan";
    $db->{"$myhash"}={};
    $db->{"$myhash"}->{name} = "Zlatan";
    $db->{"$myhash"}->{age} = "22";
    
    print "Content-type: text/html","\n\n";
    print "ok!\n
    
    ";
    
    exit(0);
    Se vuoi un consiglio da amico, evita dbm::deep, è stralento. Io ho fatto questo errore e sai anche dove XD .

    Considera che è scritto un PurePerl.

    ciao

  3. #3
    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:

    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);
    vedrai che la struttura che hai creato è la seguente:

    codice:
    $VAR1 = bless( {
                     'Zlatan' => bless( {
                                          'age' => '22'
                                        }, 'DBM::Deep' )
                   }, 'DBM::Deep' );
    come vedi ha incamerato solo il secondo elemento 'age' sovrascrivendo 'name'.

    ora prova ad usare questo codice:

    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);
    vedrai che la struttura sarà quella voluta:

    codice:
    $VAR1 = bless( {
                     'Zlatan' => bless( {
                                          'age' => '22',
                                          'name' => 'Zlatan'
                                        }, 'DBM::Deep' )
                   }, 'DBM::Deep' );
    e che tutto funzionerà, grazie al fatto che hai indicato a DBM::Deep di costruire una struttura adatta.
    Marco Allegretti
    shishii@tiscalinet.it
    Lang: PERL, PHP, SQL.
    Linux user n° 268623 Fedora Core 10, Fedora Core 6, Debian Sarge on mips

  4. #4
    dimenticavo... in fase di debug di script che usano file binari per la conservazione dei dati è molto utile usare un editor esadecimale che consente di leggere il file binario e rendersi conto di quello che c'è dentro.
    Marco Allegretti
    shishii@tiscalinet.it
    Lang: PERL, PHP, SQL.
    Linux user n° 268623 Fedora Core 10, Fedora Core 6, Debian Sarge on mips

  5. #5
    Grazie a tutti e due!!!

    E' proprio come diceva Kintaro:

    Imparo,Imparo,Imparo


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.