开发者

perl - setting up conditions to find correct key in a hash

开发者 https://www.devze.com 2023-04-09 10:15 出处:网络
Problem: Seeing exists argument is not a HASH or ARRAY element Need help setting up several conditions to grab the right key.

Problem: Seeing exists argument is not a HASH or ARRAY element Need help setting up several conditions to grab the right key.

Code: (I'm not sure also if my conditions are set up correctly. Need advice troubleshooting)

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 );

My Data: Data structure for xml file with FileNotFound:

$VAR1 = {
          'file' => {},
          'path' => '/source/feeds/customer/testA',
          'class' => 'java.io.FileNotFoundException',
          'message' => '/source/feeds/customer/testA: No such file or directory.'
        };

Data structure for xml file found:

$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:4开发者_如何学Go8:41+0000',
                        'size' => '135860644',
                        'group' => '',
                        'accesstime' => '2011-09-30T02:48:41+0000'
                      },


The interpreter is probably thinking you meant:

exists($data->{class}=~/FileNotFound/)  

Try:

exists $data->{class} and $data->{class}=~/FileNotFound/

instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消