Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

troubleshooting "pseudo-hashes are deprecated" while using xml module

Tags:

hash

perl

I am just learning how to use perl hashes and ran into this message in perl. I am using XML::Simple to parse xml output and using exists to check on the hash keys.

Message: Pseudo-hashes are deprecated at ./h2.pl line 53. Argument "\x{2f}\x{70}..." isn't numeric in exists at ./h2.pl line 53. Bad index while coercing array into hash at ./h2.pl line 53.

I had the script working earlier with one test directory and then executed the script on another directory for testing when I got this message. How do I resolve/workaround this?

Code that the error references:

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

#my $data = XMLin($xml);
my $data = XMLin($xml, ForceArray => [qw (file) ]);
my $size=0;

if (exists $data->{class}
       and $data->{class}=~ /FileNotFound/) {
        print "The directory: $Path does not exist\n";
        exit;
          } elsif (exists $data->{file}->{path}
                      and $data->{file}->{path} =~/test-out-00/) {
                    $size=$data->{file}->{size};
                       if ($size < 1024000) {
                          print "FILE SIZE:$size BYTES\n";
                          exit;
                       }
          } else {
            exit;
}

print Dumper( $data );

Working test case, data structure looks like this:

$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-00',
                        'modified' => '2011-09-30T02:48:41+0000',
                        'size' => '135860644',
                        'group' => '',
                        'accesstime' => '2011-09-30T02:48:41+0000'
                     'modified' => '2011-09-30T02:48:41+0000'
                   },
      'exclude' => ''
    };
recursive:no
version:0.20.202.1.1101050227
time:2011-10-01T07:06:16+0000
filter:.*
file:HASH(0x84c83ec)
path:/source/feeds/customer/test
directory:HASH(0x84c75d8)
exclude:

Data structure with seeing error:

$VAR1 = {
          'recursive' => 'no',
          'version' => '0.20.202.1.1101050227',
          'time' => '2011-10-03T04:49:36+0000',
          'filter' => '.*',
          'file' => [
                    {
                      'owner' => 'test_act',
                      'replication' => '3',
                      'blocksize' => '134217728',
                      'permission' => '-rw-------',
                      'path' => '/source/feeds/customer/test/20110531/test-out-00',
                      'modified' => '2011-10-03T04:47:46+0000',
                      'size' => '121406618',
                      'group' => 'feeds',
                      'accesstime' => '2011-10-03T04:47:46+0000'
                    },

Test xml file:

<?xml version="1.0" encoding="UTF-8"?><listing time="2011-10-03T04:49:36+0000" recursive="no" path="/source/feeds/customer/test/20110531" exclude="" filter=".*" version="0.20.202.1.1101050227"><directory path="/source/feeds/customer/test/20110531" modified="2011-10-03T04:48:19+0000" accesstime="1970-01-01T00:00:00+0000" permission="drwx------" owner="test_act" group="feeds"/><file path="/source/feeds/customer/test/20110531/test-out-00" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="121406618" replication="3" blocksize="134217728" permission="-rw-------" owner="test_act" group="feeds"/><file path="/source/feeds/customer/test/20110531/test-out-01" modified="2011-10-03T04:48:04+0000" accesstime="2011-10-03T04:48:04+0000" size="127528522" replication="3" blocksize="134217728" permission="-rw-------" owner="test_act" group="feeds"/><file path="/source/feeds/customer/test/20110531/test-out-02" modified="2011-10-03T04:48:19+0000" accesstime="2011-10-03T04:48:19+0000" size="125452919" replication="3" blocksize="134217728" permission="-rw-------" owner="test_act" group="feeds"/></listing>
like image 641
jdamae Avatar asked Aug 08 '26 18:08

jdamae


1 Answers

The "Pseudo-hashes are deprecated" error means you're trying to access an array as a hash, which means that either $data->{file} or $data->{file}{path} is an arrayref.

You can check the data type by using print ref $data->{file}. The Data::Dumper module may also help you to see what is in your data structure (perhaps while setting $Data::Dumper::Maxdepth = N to limit the dump to N number of levels if the structure is big).

UPDATE

Now that you are using ForceArray, $data->{file} should always point to an arrayref, which may possibly have multiple references to path. Here is a modified segment of your code to handle that. But note that the logic of the if-then-exit conditions may have to change.

if (defined $data->{class} and $data->{class}=~ /FileNotFound/) {
    print "The directory: $Path does not exist\n";
    exit;
}

exit if ! defined $data->{file};

# filter the list for the first file entry named test-out-00
my ( $file ) = grep { 
    defined $_->{path} && $_->{path} =~ /test-out-00/ 
} @{ $data->{file} };

exit if ! defined $file;

$size = $file->{size};
if ($size < 1024000) {
    print "FILE SIZE:$size BYTES\n";
    exit;
}
like image 123
stevenl Avatar answered Aug 11 '26 17:08

stevenl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!