简体   繁体   English

在Perl中是否有内置的“hash to string”?

[英]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: 我将从Python背景学习Perl,其中以下哈希到字符串转换内置于该语言中:

>>> 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. 奇怪的是,对于perl "hash to string"的网络搜索并没有出现我正在寻找的任何内容。 Thanks! 谢谢!

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

See also JSON : 另见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}

Data::Dumper模块有一种方法可以进行这种转换。

Use Data::Dump instead of Data::Dumper. 使用Data :: Dump代替Data :: Dumper。 It's basically the same, except without that annoying $VAR1 = ... cruft: 它基本上是相同的,除非没有恼人的$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. 如果你在Windows上,Data :: Dump 已从 5.8版开始预装了ActivePerl

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. 但请改用Data::Dumper

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): 如果你更喜欢某些键应该是第一个,你可以使用这种方法(我想先type第一个和第二个position ):

    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] }
            ];
    };

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. 正如Data :: Dumper的perldoc页面所述,“terse”标志可以生成非perl可解析输出。

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... 这保证是eval可以解析perl,这使得一个非常简单的方法可以对明文进行序列化...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM