我使用的是 Perl v5.16.2
我正在使用 Net::SMPP 模块,它返回一些数据。 如果我显示这些数据,我会得到这个(简化的):
$VAR1 = bless( {
'receipted_message_id' => '400002F6E09C61701222120140',
'30' => '400002F6E09C61701222120140'
}, 'Net::SMPP::PDU' );
现在,我们假设这些数据位于 $pdu 中,我这样做:
$message_id = $pdu->{30}; # or $pdu->{receipted_message_id}, same result
myfunction($message_id);
然后,我将 myfunction 定义为:
sub myfunction {
my $message_id = shift;
my $message_id_static = '400002F6E09C61701222120140';
print Dumper($message_id);
print Dumper($message_id_static);
print hexdump($message_id);
print hexdump($message_id_static);
if ($message_id eq $message_id_static)
{
print "match\n";
}
else
{
print "no match\n";
}
}
程序的输出是:
$VAR1 = '400002F6E09C61701222120140';
$VAR1 = '400002F6E09C61701222120140';
Data::Hexdumper: data length isn't an integer multiple of lines
so has been padded with NULLs at the end.
0x0000 : 34 30 30 30 30 32 46 36 45 30 39 43 36 31 37 30 : 400002F6E09C6170
0x0010 : 31 32 32 32 31 32 30 31 34 30 00 00 00 00 00 00 : 1222120140......
Data::Hexdumper: data length isn't an integer multiple of lines
so has been padded with NULLs at the end.
0x0000 : 34 30 30 30 30 32 46 36 45 30 39 43 36 31 37 30 : 400002F6E09C6170
0x0010 : 31 32 32 32 31 32 30 31 34 30 00 00 00 00 00 00 : 1222120140......
no match
这对我来说没有任何意义......! 如果我尝试使用 $message_id 执行 SQLite 查询,它会严重失败。如果我使用 $message_id_static 代替,它会完美工作。
那么,这是一个奇怪的 Perl 内部错误,还是我遗漏了什么? 这让我发疯了几个小时......
编辑:
使用 perl 调试器,我得到这个:
DB<3> x $message_id_static
0 '400002F6E09C61701222120140'
DB<4> x $message_id
0 "400002F6E09C61701222120140\c@"
所以至少我看到字符串存在差异,但为什么十六进制转储看不到它,那\c@ 是什么?
谢谢!
最佳答案
\c@
字符是 Ctrl-@,它是代码点零处的 ASCII NUL 字符
您无法在 hexdump
输出中看到它,因为它与转储末尾的 00
填充无法区分
如果您设置$Data::Dumper::Useqq = 1
,那么它将在print Dumper $message_id
的输出中可见
您可以使用 s/\0\z//
或 tr/\0//d
将其从变量中删除,但您应该真正调查原因首先是那里吗
关于string - Perl 字符串怪异 : equal strings being not equal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515425/