Cha*_*ens 40
根据您的需要,您需要使用ref或reftype(在Scalar::Util核心模块中).如果引用是一个对象,ref将返回该对象的类而不是底层引用类型,reftype将始终返回底层引用类型.
if (ref $var eq ref {}) {
print "$var is a hash\n";
}
use Scalar::Util qw/reftype/;
if (reftype $var eq reftype {}) {
print "$var is a hash\n";
}
Run Code Online (Sandbox Code Playgroud)
Iva*_*uev 15
使用ref功能:
ref($hash_ref) eq 'HASH' ## $hash_ref is reference to hash
ref($array_ref) eq 'ARRAY' ## $array_ref is reference to array
ref( $hash{$key} ) eq 'HASH' ## there is reference to hash in $hash{$key}
Run Code Online (Sandbox Code Playgroud)
我一直在使用isa,但如果被测试的东西不是对象(或者可能不是对象),则需要将其称为函数UNIVERSAL::isa:
if ( UNIVERSAL::isa( $var, 'HASH' ) ) { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26339 次 |
| 最近记录: |