Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a problem using XML simple. The xml file I get sometimes has a <status><state> tag, other times not. When it doesn't my code errors out with
Can't call method "status" on unblessed reference at
Is is possible to XML Simple with xml files that may have missing tags? I'm tempted to just use a shell call to xsltproc which handles this sort of issue, but would like to keep it all perl if possible.
use XML::Simple; use strict; my $xml = new XML::Simple(); $data = $xml->XMLin("file.xml", ForceArray => [qw( node internal )],Ke +yA ttr=> [qw( status )],); print Dumper($data); my $status=$data->status->{'state'}; if ($status eq "active"){ print "active" }

Replies are listed 'Best First'.
Re: XML::Simple giving unblessed reference
by ikegami (Patriarch) on Dec 10, 2014 at 07:05 UTC
    I think you meant to use
    $data->{status}->{state}
Re: XML::Simple giving unblessed reference
by jellisii2 (Hermit) on Dec 10, 2014 at 13:36 UTC
    If you're doing development, this will be important to read. Basically it states "don't use XML::Simple for new code".

    I can highly recommend XML::Twig. May $DEITY bless mirod.