Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: print hash into scalar

by grinder (Bishop)
on Dec 23, 2002 at 07:59 UTC ( [id://221821]=note: print w/replies, xml ) Need Help??


in reply to print hash into scalar

You could use Storable as a mechanism for serialising your hash into a scalar. I would recommend this approach as it's quite space-efficient. Much more so than, for example, Data::Dumper with $Data::Dumper::Indent = 0.

On the other hand, if Storable is space-efficient, it is because the data structure is written in a binary format. That means that you can't easily inspect the values during a debugging run, whereas you could with Data::Dumper.

Finally, Data::Dumper is dangerous for serialisation, because you recover it with an eval, which gives the bad guys a chance to Nasty Things™ to your system.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Replies are listed 'Best First'.
Re: Re: print hash into scalar
by rob_au (Abbot) on Dec 23, 2002 at 08:39 UTC
    I would strongly concur with the comments of grinder above. I would however add that care should also be taken with Storable with regard to the storage mechanism employed by this module for cross-platform compatibility.

    If the deserialisation of stored information is being performed across multiple platforms, it is important to employ the nstore routine for serialisation as this stores information in network order. Furthermore, double values are stored stringified to ensure portability, at the slight risk of losing some precision in the lowest order decimals.

    Another feature of Storable which may also be of use in caching and information retrieval is the canonical representation of stored hashes. Normally, the elements of hashes are stored in the same order as they are stored internally by Perl (eg. pseudo-randomly). This allows serialised data to be compared in their frozen form which can be useful for caching serialised information.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000000101"))'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://221821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found