Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Not a hash reference... why?

by toliyn (Novice)
on Apr 26, 2010 at 07:02 UTC ( #836882=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to learn how to use simple::xml. I have file cmdb_cohes.xml
<?xml version="1.0" encoding="utf-8" ?> <SDForm> <Form name="cmdb_cohesion.html" lang="en_US"> <ResourceString> <tag>en</tag> <value>en</value> </ResourceString> <ResourceString> <tag>Cohesion</tag> <value>Cohesion</value> </ResourceString> </Form> </SDForm>
I wrote file.pl
use XML::Simple; use strict; my $xml = new XML::Simple; my $data = $xml->XMLin('cmdb_coh.xml'); print $data->{Form}->{ResourceString}->{value};
and as a result of running this script i get such error "Not a HASH reference at: ...file.pl line 14. Please tell me why? What i need to do to fix this. thanks

Replies are listed 'Best First'.
Re: Not a hash reference... why?
by Corion (Pope) on Apr 26, 2010 at 07:08 UTC

    You have two ResourceString tags. Which tag do you expect $data->{Form}->{ResourceString} to return?

    When confronted with an unknown data structure, dumping it using Data::Dumper often helps you understand it. Also see ForceArray in the XML::Simple documentation.

      thanks
Re: Not a hash reference... why?
by derby (Abbot) on Apr 26, 2010 at 07:13 UTC

    use strict; use warnings; use Data::Dumper; use XML::Simple; my $xml = new XML::Simple; my $data = $xml->XMLin('cmdb_coh.xml'); print Dumper( $data );

    -derby
Re: Not a hash reference... why?
by toliyn (Novice) on Apr 26, 2010 at 07:13 UTC
    I get it.
    use XML::Simple; use strict; my $xml = new XML::Simple; my $data = $xml->XMLin('cmdb_coh.xml'); print $data->{Form}->{ResourceString}->[1]->{value};
Re: Not a hash reference... why?
by BrowserUk (Pope) on Apr 26, 2010 at 07:11 UTC
    Not a HASH reference at: ...file.pl line 14. Please tell me why?

    That's kinda hard to do since your posted script only contains 5 lines...

    I guess we could download your xml, & put it in a file with the right name. Download your script put it in another file. Install XML::Simple; and then run the script to see what error it actually produces and where...


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Not a hash reference... why?
by arc_of_descent (Hermit) on Apr 26, 2010 at 16:39 UTC

    I prefer to use the debugger when I need to inspect data structures. The x command is excellent for this. It can inspect arrays, hashes as well as objects. You can then use the p command to print out your references and check whether they are fine.

    perldebug

Re: Not a hash reference... why?
by Anonymous Monk on Apr 26, 2010 at 09:22 UTC
    Hello! For tracking data structure i recommend you to use Data::Dumper module. Just do something like below: .... use Data::Dumper; use XML::Simple; use strict; my $xml = new XML::Simple; my $data = $xml->XMLin('cmdb_coh.xml'); print Dumper($data),"\n"; And then inspect your data structure

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://836882]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2021-05-16 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (151 votes). Check out past polls.

    Notices?