Reputation: 71424
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!
Upvotes: 19
Views: 28060
Reputation: 4883
See also JSON:
#!/usr/bin/perl
use warnings; use strict;
use JSON;
my $data = {a => 1, b=> 2, c => 3};
print to_json($data);
This produces:
{"c":3,"a":1,"b":2}
Upvotes: 31
Reputation: 1156
Several of the above solutions have a problem if you have the potential for multi-level structures.
Specifically this flag:
$Data::Dumper::Terse = 1;
As noted on the perldoc page for Data::Dumper, the "terse" flag could generate non-perl parseable output.
If you possibly are going to have multi-depth structures the proper thing to do would be to instead use:
$Data::Dumper::Indent = 0;
Which is guaranteed to be perl parseable by eval, which makes for a very very easy way of doing serialization to plaintext...
Upvotes: 2
Reputation: 26121
Yet Another Swallow Solution:
sub pp {
my $h = shift();
qq[{${\(join',',map"$_=>$h->{$_}",keys%$h)}}]
}
print pp({a => 1, b => 2, c => 3});
But use Data::Dumper
instead.
For very fancy output you can use also:
use Data::Dumper;
use Perl::Tidy;
sub pp {
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
my $source = Dumper(@_);
my $result;
Perl::Tidy::perltidy(
source => \$source,
destination => \$result,
argv => [qw(-pbp -nst)]
);
return $result;
}
If you prefer some keys should be first than you can use this approach (i want type
first and position
second):
local $Data::Dumper::Sortkeys = sub {
[ sort {
if ( $b eq 'type' ) {1}
elsif ( $a eq 'type' ) {-1}
elsif ( $b eq 'position' ) {1}
elsif ( $a eq 'position' ) {-1}
else { $a cmp $b }
} keys %{ $_[0] }
];
};
Upvotes: 2
Reputation: 51226
Use Data::Dump instead of Data::Dumper. It's basically the same, except without that annoying $VAR1 = ...
cruft:
use Data::Dump "pp";
print pp({a => 1, b => 2, c => 3});
Produces:
{ a => 1, b => 2, c => 3 }
If you're on Windows, Data::Dump has come pre-installed with ActivePerl since version 5.8.
Upvotes: 9
Reputation: 30225
use Data::Dumper;
local $Data::Dumper::Terse = 1;
my $str = Dumper({a => 1, b => 2, c => 3});
Upvotes: 44
Reputation: 993085
There is the Data::Dumper
module which one way to do this sort of transformation.
Upvotes: 11