Re: A neat trick to serialize arrays and hashes

From: Matt Garrish (matthew.garrish_at_sympatico.ca)
Date: 06/18/04


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



Relevant Pages

  • Re: adding data to a file before it gets regexed
    ... hashes, but I am open to using them. ... The reasion I am trying to add the string to the line, is so that I can use ... it is not a good idea to store entire files in memory and it is really ... the arrays are the contents of the files broken down by lines. ...
    (perl.beginners)
  • Re: Boolschen Kontext forcieren
    ... Bei Strings ist der leere String false, ... Arrays und Hashes werden im skalaren Kontext interpretiert. ... Next by Date: ...
    (de.comp.lang.perl.misc)
  • Re: Boolschen Kontext forcieren
    ... > Bei Strings ist der leere String false, ... > Arrays und Hashes werden im skalaren Kontext interpretiert. ... Kontext ausgewertet wird. ...
    (de.comp.lang.perl.misc)
  • Hashes of Hashes
    ... I'm working on a complex data structure with hashes of hashes and ... hashes of arrays and I noticed a weird behavior I was hoping someone ... group = STRING ... If I swap the order of the declarations, then all of the keys for the ...
    (perl.beginners)
  • Re: A neat trick to serialize arrays and hashes
    ... This technique only handles simple arrays and hashes. ... > lists of lists, ... > an array of these serialized arrays, ...
    (comp.lang.perl.misc)

Loading