如何设置默认值?

San*_*ing 1 perl hash

假设我有这样的哈希值

my %profile = (
    building            => $p->{account}->{building},
    email               => $p->{account}->{email},
    phone               => $p->{account}->{phone},
    );
Run Code Online (Sandbox Code Playgroud)

变量in $p尚未定义时可以包含各种值.我至少看过了undef ~ ''.

如何分配值,-1例如$profile{building}是否$p->{account}->{building}具有这些奇怪的默认值之一?

有没有聪明的Perl方法呢?

更新:任何值都可以采用任何奇怪的默认值undef ~ ''.

小智 6

我会添加一个功能:

my %profile = (
    building            => scrub($p->{account}->{building}),
    email               => scrub($p->{account}->{email}),
    phone               => scrub($p->{account}->{phone}),
    );
Run Code Online (Sandbox Code Playgroud)

并在函数中实现默认过滤逻辑.

或者,更好的是,将逻辑预先应用于$ p,以便您知道$ p具有合理的值.