我有一种情况,我想创建一个数据结构的签名:
- my $signature = ds_to_sig(
- { foo => 'bar',baz => 'bundy',boing => undef,number => 1_234_567,}
- );
目的应该是,如果数据结构发生变化,那么应该签名.
有没有确定的方法来做到这一点?
解决方法
最好的方法是使用像
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,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;