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

Surely I'm doing something wrong here! There is now way a 27 line XML file can bring XML::Simple to it's knees.

When I run this code:

use strict; use warnings; use XML::Simple; my $associate = XMLin('./amazon.xml', forcearray=>0); my $books = $associate->{books}->{item}; my $cds = $associate->{cds}->{item}; my $dvds = $associate->{dvds}->{item}; my $wish = $associate->{ wish}->{item}; foreach my $books (values %$books) { print "$books->{'asin'} \n"; }

I get this error:  Out of memory!

Here is my XML file:

<associate> <books> <item item_name="Mysql and Perl for the Web" asin="0735710546" + /> <item item_name="Macromedia Flash MX: Training from the Source +" asin="0201794829" /> <item item_name="XSLT" asin="0596000537" /> <item item_name="Learning XML" asin="0596000464" /> <item item_name="Programming Perl (3rd Edition)" asin="0596000 +278" /> <item item_name="Network Programming with Perl" asin="02016157 +11" /> <item item_name="Cisco Networking Academy Program: Fundamental +s of UNIX Companion Guide" asin="1587130440" /> </books> <dvds> <item item_name="Gladiator (2000)" asin="B00003CXE7" /> <item item_name="The Princess Bride (Special Edition) (1987)" +asin="B00005LOKQ" /> <item item_name="Pi" asin="078401213X" /> </dvds> <cds> <item item_name="OK Computer (Radiohead)" asin="B000002UJQ" /> <item item_name="Pablo Honey (Radiohead)" asin="B000002UR7" /> <item item_name="Parachutes (Coldplay)" asin="B0000508U6" /> <item item_name="Hatful of Hollow (The Smiths)" asin="B000002M +IF" /> </cds> <wish> <item item_name="The Usual Suspects (Special Edition) (1995)" +asin="B00005V9HH" /> <item item_name="How to Win Friends and Influence People" asin +="0671723650" /> <item item_name="ActionScript for Flash MX: The Definitive Gui +de, Second Edition " asin="059600396X" /> </wish> </associate>


Windows 2000
perl 5.6.1
XML::Simple 1.06


-Silent11

Replies are listed 'Best First'.
Re: 'Out of memory!' Error with XML::Simple
by gjb (Vicar) on Jan 14, 2003 at 22:18 UTC

    I don't know why such an amount of memory is used (I can only assume XML::Simple gets very confused), but I do know how to deal with it.

    Using Data::Dumper I noticed that $books is not a hashref, but a listref. Replacing the foreach with:

    foreach my $book (@$books) { print "$book->{'asin'} \n"; }
    solves the problem.

    Hope this helps, -gjb-

Re: 'Out of memory!' Error with XML::Simple
by grantm (Parson) on Jan 14, 2003 at 23:32 UTC

    This is covered in the XML::Simple FAQ. The cause of the problem is that you're treating an arrayref as if it was a hashref (as gjb pointed out).

    There are a few more things worthy of consideration and I'd recommend you take a look at Does your XML::Simple code pass the strict test? for more complete coverage. But in brief:

    • you are not setting a value for the keyattr option (if you don't know what it does, set keyattr => [])
    • you are turning off forcearray which is a bad idea for two reasons:
      1. your code will break if you only have one <item>
      2. enabling array folding (which you're doing by letting keyattr default) is inherently incompatible with disabling forcearray
    • your <item> elements have an 'item_name' attribute where 'name' might be a more natural choice - I'm guessing you tried using name but "weird stuff happened" (because you didn't set keyattr to disable array folding)

    The 'strict mode' link above has more detail.

Re: 'Out of memory!' Error with XML::Simple
by traveler (Parson) on Jan 14, 2003 at 22:26 UTC
    gjb hit the nail on the head. Put a call to exit before the loop and it does not run out of memory. The issue is the loop. The fixes gjb suggested do the job.

    --traveler