Re: A neat trick to serialize arrays and hashes
From: Matt Garrish (matthew.garrish_at_sympatico.ca)
Date: 06/18/04
- Next message: Matt Garrish: "Re: Problem with DOS paths in perl - cmd.exe vs. command.exe"
- Previous message: Purl Gurl: "Re: Problem with DOS paths in perl - cmd.exe vs. command.exe"
- In reply to: J. Romano: "A neat trick to serialize arrays and hashes"
- Next in thread: J. Romano: "Re: A neat trick to serialize arrays and hashes"
- Reply: J. Romano: "Re: A neat trick to serialize arrays and hashes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 17 Jun 2004 23:51:57 -0400
"J. Romano" <jl_post@hotmail.com> wrote in message
news:b893f5d4.0406171932.376394cf@posting.google.com...
>
<snip explanation of packing>
>
> There are a few important items to point out:
>
> 1. The serialized string will most likely contain
> non-printable characters, which may include some
> newline characters, even if no scalar in the
> original array/hash contains a "\n" character.
> Because of this, you should use the binmode()
> function on any filehandle you plan to print the
> string out to.
>
> 2. If the array or hash contains any numbers, they
> will be converted to their string representation.
>
> 3. This technique only handles simple arrays and hashes.
> In other words, multi-dimensional arrays and hashes,
> lists of lists, an references are not handled
> correctly. If you really want to serialize a
> complex structure such as one of these, I recommend
> using another approach, like taking advantage of
> the Data::Dumper module. You CAN however, create
> an array of these serialized arrays, and serialize
> that array!
>
> 4. The "w" in the pack string "(w/a*)*" allows for the
> encoding of any arbitrary-length string, even if it
> is longer than 0xffffffff bytes (4,294,967,295
> bytes). But since "w" is only used for encoding
> non-negative integers, the "(w/a*)*" pack string
> cannot be used to encode arrays or hashes
> containing negative-length strings. Fortunately,
> that's never been a problem for me. :)
>
> 5. I do not know if this trick can handle arrays
> and hashes containing Unicode strings. My guess
> is that it can, but I haven't tested it so I can't
> say for sure.
>
Sorry to rain on your parade, but with all the caveats don't you think it
would be better just to use the Storable module, especially since it's part
of the core distribution? Better techniques are worth noting for the simple
reason that they're better...
Matt
- Next message: Matt Garrish: "Re: Problem with DOS paths in perl - cmd.exe vs. command.exe"
- Previous message: Purl Gurl: "Re: Problem with DOS paths in perl - cmd.exe vs. command.exe"
- In reply to: J. Romano: "A neat trick to serialize arrays and hashes"
- Next in thread: J. Romano: "Re: A neat trick to serialize arrays and hashes"
- Reply: J. Romano: "Re: A neat trick to serialize arrays and hashes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|