perl – 为什么undef变成空字符串?

前端之家收集整理的这篇文章主要介绍了perl – 为什么undef变成空字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
use strict;
use warnings;
use Data::Dumper;

my %h;
my $undef = undef;
$h{''}='test2';
$h{$undef} = 'test';

print Dumper (\%h);

创建以下输出

$VAR1 = {
          '' => 'test'
        };

为什么会这样?
我有Perl 5.12.3.

谢谢你的时间.

解决方法

所有哈希键都是字符串.用作散列键的非字符串值被强制转换为字符串,undef在该上下文中变为“”.

猜你在找的Perl相关文章