Perl 如何查找传递给函数的参数类型

Perl 如何查找传递给函数的参数类型,perl,Perl,子行正在获取哈希引用: sub test { my $hash_ref = shift ; if ( $hash_ref->{app} ) { ... } hash-ref的格式如下:如何找出它所拥有的数据类型 #scallar $hash {app} = 'app' ; (or) #array $hash {app} = ['app1' ,'app2' ,'app3']; (or) #hash $hash {app} = { app1 => { t

子行正在获取哈希引用:

sub test { 
  my $hash_ref = shift ; 

   if ( $hash_ref->{app} ) {
   ... 

   }
hash-ref的格式如下:如何找出它所拥有的数据类型

#scallar
$hash {app} = 'app' ;
(or)
#array
$hash {app} = ['app1' ,'app2' ,'app3'];
(or)
#hash
$hash {app} = { app1 => { type => 1, contact=> abc }}
(or)
#array +hash 
$hash {app} = [{ app1 => { type => 1, contact=> abc }} ,
               { app2 => { type => 2, contact=> ded }}]

如何处理这种类型的数据结构

如果您提供了一个参考,您可以通过以下方式检查该类型:

打印
数组

请看:

use strict;
use warnings;

my $hash1 = {key => 'app',};
my $hash2 = {key => ['app1', 'app2'],};
my $hash3 = {key => {app1 => {type => 1, contact => 'abc'}},};
my $hash4 = {key => [{app1 => {type => 1, contact => 'abc'}}, {app2 => {type => 2, contact => 'ded'}}],};
my %tests = (1 => $hash1, 2 => $hash2, 3 => $hash3, 4 => $hash4);

while (my ($test_nr, $test_hash) = each %tests) {
    if (!ref $test_hash->{key}) {
        print "test $test_nr is scalar\n";
    } elsif (ref $test_hash->{key} eq 'HASH') {
        print "test $test_nr is hash ref\n";
    } elsif (ref $test_hash->{key} eq 'ARRAY') {
        if (ref $test_hash->{key}[0]) {
            print "test $test_nr is array of hash refs\n";
        } else {
            print "test $test_nr is array\n";
        }
    }
}
use strict;
use warnings;

my $hash1 = {key => 'app',};
my $hash2 = {key => ['app1', 'app2'],};
my $hash3 = {key => {app1 => {type => 1, contact => 'abc'}},};
my $hash4 = {key => [{app1 => {type => 1, contact => 'abc'}}, {app2 => {type => 2, contact => 'ded'}}],};
my %tests = (1 => $hash1, 2 => $hash2, 3 => $hash3, 4 => $hash4);

while (my ($test_nr, $test_hash) = each %tests) {
    if (!ref $test_hash->{key}) {
        print "test $test_nr is scalar\n";
    } elsif (ref $test_hash->{key} eq 'HASH') {
        print "test $test_nr is hash ref\n";
    } elsif (ref $test_hash->{key} eq 'ARRAY') {
        if (ref $test_hash->{key}[0]) {
            print "test $test_nr is array of hash refs\n";
        } else {
            print "test $test_nr is array\n";
        }
    }
}