Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

XML::Complicated

by hacker (Priest)
on Apr 30, 2003 at 22:15 UTC ( [id://254501]=perlquestion: print w/replies, xml ) Need Help??

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

In the process of working on another problem, I found the 'keyattr' bit of XML::Simple, and the FAQ that talks about 'array folding'. I cooked up this small snippet, which causes XML::Simple to throw an Out of memory! error. If I set 'forcearray' to 0, disabling it (against the recommended behavior), the error goes away.

What I want to know is.. why?

Obligatory Code

use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = XMLin(\*DATA, keyattr => ['url'], forcearray => '1'); print Dumper($xml); print "URL: $xml->{bar}->{blort}->{quux}->{url}\n"; __DATA__ <?xml version="1.0" encoding="iso-8859-2"?> <foo> <bar> <blort> <quux> <url>http://f.co/index.html</url> </quux> </blort> </bar> </foo>

Replies are listed 'Best First'.
Re: XML::Complicated
by Chmrr (Vicar) on Apr 30, 2003 at 23:00 UTC

    This is a bug in Perl relating to pseudohashes, and is fixed in 5.9.0-to-be. There, it gives the more edifying message "Not a HASH reference at ..."

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: XML::Complicated
by grantm (Parson) on Apr 30, 2003 at 22:39 UTC
    I cooked up this small snippet, which causes XML::Simple to throw an Out of memory!

    No, Perl stops with an out of memory error when you treat an arrayref as a hashref. The print Dumper($xml) line output clearly shows that if you turn on forcearray, you get arrays.

(jeffa) Re: XML::Complicated
by jeffa (Bishop) on May 01, 2003 at 13:29 UTC
    The reason i used the keyattr in my previous reply was due to a strange quirk within XML::Simple. Consider this example:
    use XML::Simple; use Data::Dumper; my $xml = XMLin(\*DATA); print Dumper $xml; __DATA__ <documents> <document> <stayonhost>1</stayonhost> <staybelow>1</staybelow> <maxdepth>2</maxdepth> <name>Swedish Foo</name> <home_url>http://www.foo.se/bar/</home_url> <category>International</category> </document> <document> <stayonhost>0</stayonhost> <staybelow>0</staybelow> <maxdepth>3</maxdepth> <name>Swedish Bar</name> <home_url>http://www.bar.se/baz/</home_url> <category>National</category> </document> </documents>
    and it's output:
    $VAR1 = {
      'document' => {
        'Swedish Foo' => {
          'category' => 'International',
          'maxdepth' => '2',
          'stayonhost' => '1',
          'staybelow' => '1',
          'home_url' => 'http://www.foo.se/bar/'
        },
        'Swedish Bar' => {
          'category' => 'National',
          'maxdepth' => '3',
          'stayonhost' => '0',
          'staybelow' => '0',
          'home_url' => 'http://www.bar.se/baz/'
        }
      }
    };
    
    What happened to the name element? I don't know for sure, but i am guessing that XML::Simple treats elements named name differently.* So, i read some docs, figured that keyattr might fix the problem and it did. But first i tried forcearray, which didn't fix the problem. I didn't think it would, but i didn't have a spare chicken to sacrifice at the time. ;) In case you are wondering ... forcearray is useful for keeping XML elements in order, remember that hashes lose order. forcearray is also useful for keeping your code consistent - you can always (99.99%?) expect an array, no guessing with ref needed.

    I guess my point is that XML::Simple can handle tasks that are really beyond its scope, and you should be wary of that. It's great for getting stuff done quick, but you probably should pick a more industrial strength module for serious production code - like XML::Twig.

    * yep ... here's what the docs say:
       The default value for 'keyattr' is ['name', 'key',
       'id'].  Setting this option to an empty list will
       disable the array folding feature.
    

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Just posting to second the notion to use XML::Twig. I don't even bother with XML::Simple these days.

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-19 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found