我有一种情况,我想创建一个数据结构的签名:
my $signature = ds_to_sig(
{ foo => 'bar',
baz => 'bundy',
boing => undef,
number => 1_234_567,
}
);
目标应该是,如果数据结构发生变化,那么签名也应该发生变化。
有没有一种既定的方法来做到这一点?
最佳答案
最好的方法是使用像 Storable 这样的深层结构序列化系统。 .具有相同数据的两个结构将产生相同的 Storable 输出 blob,因此可以比较它们。
#!/usr/bin/perl
use strict;
use warnings;
use Storable ('freeze');
$Storable::canonical = 1;
my $one = { foo => 42, bar => [ 1, 2, 3 ] };
my $two = { foo => 42, bar => [ 1, 2, 3 ] };
my $one_s = freeze $one;
my $two_s = freeze $two;
print "match\n" if $one_s eq $two_s;
...并证明逆:
$one = [ 4, 5, 6 ];
$one_s = freeze $one;
print "no match" if $one_s ne $two_s;
关于perl - 如何创建数据结构的可重复签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/218531/