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

How can I convert a hash (%hash) to a string ($string) such as so ($string) can be converted back to (%hash)?

Im currently designing a milter using Sendmail::PMilter , but the getpriv/setpriv can only store one single value for state tracking across callbacks, so to store "multiple" values into this, I need to collapse the hash into a string, and then expand the string when being used/changed.

I googled and found multiple examples of Data::Dump and Data::Dumper, but that only allows one-way conversion from hash to string, not back again.

Note that the conversion function must be "safe" against anything that can be present in strings and hashes.

  • Comment on How can I convert hash to string and back again?

Replies are listed 'Best First'.
Re: How can I convert hash to string and back again?
by roboticus (Chancellor) on Mar 02, 2015 at 20:19 UTC

    sebastiannielsen2:

    I've been using JSON for persisting data structures lately, and am pretty happy with it. (One reason is that we're a multilanguage shop, and plenty of systems use JSON for I/O.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: How can I convert hash to string and back again?
by LanX (Saint) on Mar 02, 2015 at 20:37 UTC
    > but that only allows one-way conversion 

    That's not correct, both are designed to produce perl code which can be evaled . Furthermore is Data::Dumper a core module.

    Though there are traps when dealing with special structures like objects and recursive references (similar with JSON)

    As long as you are only dealing with hashes, arrays and "normal" scalars (like strings) you should be more than fine.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

      Is Data::Dump/Data::Dumper safe with unsanitized user input/unsafe data? Im feeding email data directly into a hash. Dumping the hash is no problem, but undumping it with eval. Is Data::Dump/Data::Dumper making sure any code inside Always is completely 100% safe to run - provided I only deal with hashes, strings and arrays?

        > safe with unsanitized user input/unsafe data?

        Good question ...

        lets test:

        DB<103> $hash={ key => ' text @{[print "Injection" ]} text' } => { key => " text \@{[print \"Injection\" ]} text" } DB<104> use Data::Dumper DB<105> $str = Dumper $hash $VAR1 = { 'key' => ' text @{[print "Injection" ]} text' }; DB<106> eval $str => { key => " text \@{[print \"Injection\" ]} text" } DB<108> print $VAR1->{key} text @{[print "Injection" ]} text

        Looks fine for me. =)

        update

        Explanation: Data::Dumper puts strings into single quotes, so no danger of interpolation.

        Data::Dump uses double quotes, but escapes all sigils.

        update

        NB: eval of included strings can still be dangerous! They don't sanitize dangerous strings for you, they will just reproduce the original data structure.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        PS: Je suis Charlie!

Re: How can I convert hash to string and back again?
by kennethk (Abbot) on Mar 02, 2015 at 20:28 UTC
    As roboticus said, JSON is probably the right choice. Data::Dump and Data::Dumper both work with eval as the unfreeze operation.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: How can I convert hash to string and back again?
by Anonymous Monk on Mar 03, 2015 at 00:50 UTC