user2412986
user2412986

Reputation: 11

Perl Error : Not an ARRAY reference

I am trying to execute the following code, but it gives the error code :

use XML::Simple;
use Data::Dumper;

$xml = new XML::Simple (KeyAttr=>[]);# read XML file

$error =$xml->XMLin("trial.xml");
print "There are " . scalar@{$error->{problem}} . " problems.\n";

so it gives error prescribed at line :

print "There are " . scalar@{$error->{problem}} . " problems.\n";

Please let me know what I am doing wrong. thank you.

Upvotes: 1

Views: 1401

Answers (2)

mirod
mirod

Reputation: 16151

As per the error message, $error->{problem} is not an array reference. The usual cause for this is that there is only one problem under error, as opposed to several, in which case XML::Simple doesn't generate an array.

Look for the ForceArray option in the docs: https://metacpan.org/module/GRANTM/XML-Simple-2.20/lib/XML/Simple.pm#ForceArray-1-in

Upvotes: 3

Matthias
Matthias

Reputation: 1005

Take a look at the documentation:

ERROR HANDLING

The XML standard is very clear on the issue of non-compliant documents. An error in parsing any single element (for example a missing end tag) must cause the whole document to be rejected. XML::Simple will die with an appropriate message if it encounters a parsing error.

If dying is not appropriate for your application, you should arrange to call XMLin() in an eval block and look for errors in $@. eg:

my $config = eval { XMLin() };
PopUpMessage($@) if($@);

regards, Matthias

Upvotes: 0

Related Questions