perl - 设置条件以在哈希中找到正确的密钥

jda*_*mae 0 perl hash

问题: 看到exists argument is not a HASH or ARRAY element 需要帮助设置几个条件以获取正确的键.

代码:(我也不确定我的条件是否正确设置.需要建议故障排除)

my $xml = qx(@cmdargs);
my $data = XMLin($xml);
my $size=0;

# checking for error string, if file not found then just exit
# otherwise check the hash keys for filename and get its file size
if (exists $data->{class} =~ /FileNotFound/) {
       print "The directory: $Path does not exist\n";
        exit;
          } elsif (exists $data->{file}->{path}
                      and $data->{file}->{path} =~/test-out-XXXXX/) {   
                    $size=$data->{file}->{size};
                      print "FILE SIZE:$size\n";
          } else {
        #    print  "Nothing to print.\n";
     }
     # print "$data"; 
print Dumper( $data );
Run Code Online (Sandbox Code Playgroud)

我的数据: xml文件的数据结构FileNotFound:

$VAR1 = {
          'file' => {},
          'path' => '/source/feeds/customer/testA',
          'class' => 'java.io.FileNotFoundException',
          'message' => '/source/feeds/customer/testA: No such file or directory.'
        };
Run Code Online (Sandbox Code Playgroud)

找到的xml文件的数据结构:

$VAR1 = {
              'recursive' => 'no',
              'version' => '0.20.202.1.1101050227',
              'time' => '2011-09-30T02:49:39+0000',
              'filter' => '.*',
              'file' => {
                        'owner' => 'test_act',
                        'replication' => '3',
                        'blocksize' => '134217728',
                        'permission' => '-rw-------',
                        'path' => '/source/feeds/customer/test/test-out-00000',
                        'modified' => '2011-09-30T02:48:41+0000',
                        'size' => '135860644',
                        'group' => '',
                        'accesstime' => '2011-09-30T02:48:41+0000'
                      },
Run Code Online (Sandbox Code Playgroud)

小智 7

口译员可能会认为你的意思是:

exists($data->{class}=~/FileNotFound/)  
Run Code Online (Sandbox Code Playgroud)

尝试:

exists $data->{class} and $data->{class}=~/FileNotFound/
Run Code Online (Sandbox Code Playgroud)

代替.