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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/459668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:22:11  来源:igfitidea点击:

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

stringperlhash

提问by cdleary

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. Thanks!

奇怪的是,网络搜索perl "hash to string"并没有找到我正在寻找的任何东西。谢谢!

回答by Leon Timmermans

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

回答by dwarring

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}

回答by Greg Hewgill

There is the Data::Dumpermodule which one way to do this sort of transformation.

有一个Data::Dumper模块可以通过哪种方式进行这种转换。

回答by j_random_hacker

Use Data::Dumpinstead of Data::Dumper. It's basically the same, except without that annoying $VAR1 = ...cruft:

使用Data::Dump而不是 Data::Dumper。它基本上是一样的,除了没有那些烦人的$VAR1 = ...杂物:

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 ActivePerlsince version 5.8.

如果您使用的是 Windows,Data::Dump从 5.8 版开始就预装在ActivePerl 中

回答by Hynek -Pichi- Vychodil

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::Dumperinstead.

而是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 typefirst and positionsecond):

如果你更喜欢一些键应该是第一个,而不是你可以使用这种方法(我想要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] }
            ];
    };

回答by kamelkev

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 页面所述,“简洁”标志可以生成非 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 解析,这为将序列化为纯文本提供了一种非常简单的方法......