muba has asked for the wisdom of the Perl Monks concerning the following question:

SDBM_File seems not to be able to store complex data stuctures, such as key => [1, 2, 3]. When I untie the hash and then retie it from another script to read from it, it tells me that 'key' is a ref to an array, but I can not get the contents of that array.

Is there any workaround?




"2b"||!"2b";$$_="the question"
Besides that, my code is untested unless stated otherwise.
One more: please review the article about regular expressions (do's and don'ts) I'm working on.

Replies are listed 'Best First'.
Re: SDBM_File and complex data structures
by PodMaster (Abbot) on Oct 10, 2004 at 12:34 UTC
    No hash can really store complex data structures, they can store references to them, which is why when SDBM_File serializes the data structure, what gets stored as a value instead of a reference is its string value (print [] prints ARRAY(0x225240), but $a="ARRAY(0x225240)"; is not an array reference).
    What you should investigate is MLDBM (pay attention to the BUGS ).
    it tells me that 'key' is a ref to an array
    No it doesn't. `perldoc -f ref'

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I see. Ok, I will check MLDBM.

      Indeed, that print statement did not explicitly tell me it was a ref to an array, technically, but I would then like to call it... "ref at first sight" :)

      Well, thank you for your quick reply.




      "2b"||!"2b";$$_="the question"
      Besides that, my code is untested unless stated otherwise.
      *One more: please review the article about regular expressions (do's and don'ts) I'm working on.
      MLDBM and SDBM_File is a dangerous combination because the former creates long values out of nested data structure and the latter truncates those, resulting in data loss.

      In this situation I'd recommend a safer solution. Like DBM::Deep.

Re: SDBM_File and complex data structures
by pg (Canon) on Oct 10, 2004 at 17:04 UTC

    If all what you want is to share data between scripts, Storable is also a candidator. The following scripts show that Storable handles the data type you have: (your data is frozen by an UDP client, sending to an UDP server, and the server thaw it, still get the same thing.)

    Client: use Storable qw(freeze); use IO::Socket; use strict; use warnings; my $c = new IO::Socket::INET(Proto => "udp", PeerAddr => "localhost", PeerPort => 3000, Timeout => 1); print "Connected\n"; my $h = {"key" => [1,2,3,4,5,6,7,8,9,10]}; my $arr_freezed = freeze($h); $c->send($arr_freezed); close $c; Server: use IO::Socket; use Storable qw(thaw); use Data::Dumper; use strict; use warnings; my $server = new IO::Socket::INET(Timeout => 7200, Proto => "udp", LocalPort => 3000, LocalHost => "localhost"); print "Server is listening ...\n"; my $arr_freezed; $server->recv($arr_freezed, 1000); my $h = thaw($arr_freezed); print Dumper($h); close $server;