cdleary cdleary - 5 months ago 10
Perl Question

Is there a builtin "hash to string" in Perl?

I'm coming to learn Perl from a Python background where the following hash-to-string conversion is built in to the language:

>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> str(d)
"{'a': 1, 'c': 3, 'b': 2}"


Is there a builtin and/or module that has a subroutine with output along the lines of:

"('a' => 1, 'b' => 2, 'c' => 3)"


Strangely, a web search for
perl "hash to string"
doesn't turn up anything along the lines I'm looking for. Thanks!

Answer
use Data::Dumper;
local $Data::Dumper::Terse = 1;
my $str = Dumper({a => 1, b => 2, c => 3});