perlmeditation grantm <p>Version 2.0 of [cpan://XML::Simple] has just been released. The major version number increment is because this is the first stable version with SAX support, but today's essay has nothing to do with SAX. A new feature in this release is 'strict mode' which you can enable like this:</p> <code> use XML::Simple qw(:strict); </code> <readmore> <p>Let's toss it into a simple test script ...</p> <code> use strict; use XML::Simple qw(:strict); my $xml =q(<?xml version='1.0'?> <library> <book> <isbn>0596001320</isbn> <title>Learning Perl, 3rd Edition</title> <author>Randal L. Schwartz</author> <author>Tom Phoenix</author> </book> <book> <isbn>1565922204</isbn> <title>Advanced Perl Programming</title> <author>Sriram Srinivasan</author> </book> <book> <isbn>076455106X</isbn> <title>Guitar for Dummies</title> <author>Mark Phillips</author> <author>John Chappell</author> </book> </library> ); my $library = XMLin($xml); foreach my $book (@{$library->{book}}) { print "$book->{title}\n"; print " $_\n" foreach(@{$book->{author}}); } </code> <p>When we run this script, instead of the expected list of book titles, we get this message:</p> <code> No value specified for 'forcearray' option in call to XMLin() at ./ line 26 </code> <p>A closer look at the foreach loop reveals that the test script assumes $library->{book} is an arrayref (which it is) and that each $book->{author} is also an arrayref (which the second is not). Now XML::Simple doesn't know what assumptions your script is making, but it does know that failing to specify an explicit value for the forcearray option can lead to dangerous assumptions just like the ones in our script.</p> <p>The simplest way to get past this error is to set <code>forcearray => 1</code>. Unfortunately this will have the undesirable side-effect of turning $book->{title} into an arrayref too. A better solution is to set forcearray to a list of the element names which should be forced to an array representation - even if they occur only once:</p> <code> my $library = XMLin($xml, forcearray => [ qw(book author) ]); </code> <p>Now if we run the script, it fails again but with a new message:</p> <code> No value specified for 'keyattr' option in call to XMLin() at ./ line 26 </code> <p>Once again, XML::Simple has detected a common mistake - failing to explicitly enable or disable the 'array folding' feature.</p> <p>If we wanted to select a particular &lt;book&gt; record by its &lt;isbn&gt; we could have explicitly asked for the array of &lt;book&gt;s to be 'folded' into a hash using &lt;isbn&gt; as the key:</p> <code> my $library = XMLin($xml, forcearray => [ qw(book author) ], keyattr => { book => 'isbn' } ); print $library->{book}->{1565922204}->{title}, "\n"; </code> <p>But, we actually wanted to loop through all the &lt;book&gt;s in the order they occurred in the XML, so we'll put back the original loop code and explicity disable array folding like this:</p> <code> my $library = XMLin($xml, forcearray => [ qw(book author) ], keyattr => [] ); </code> <p>Now the code runs with no more errors and produces the expected output:</p> <code> Learning Perl, 3rd Edition Randal L. Schwartz Tom Phoenix Advanced Perl Programming Sriram Srinivasan Guitar for Dummies Mark Phillips John Chappell </code> <p>Those are the two most common errors, but if we revisit the array folding version we'll use strict mode to catch a more subtle problem:</p> <code> my $library = XMLin($xml, forcearray => [ qw(author) ], keyattr => { book => 'isbn' } ); print $library->{book}->{1565922204}->{title}, "\n"; </code> <p>This time the script will die with this error:</p> <code> <book> set in keyattr but not in forcearray at ./ line 26 </code> <p>In fact, without strict mode this version of the script would work exactly as expected. There is a lurking problem though that will leap out and bite us if the XML is changed to include only one &lt;book&gt;. Without 'book' listed in the forcearray list, the lone &lt;book&gt; element would be represented as a single hashref (rather than an array of hashrefs) and would therefore not be 'folded' into a hash keyed on &lt;isbn&gt;. The answer is simple - any elements you want folded should be listed in both the keyattr hash <b>and</b> the forcearray array.</p> <p>Another type of data error can also be trapped by strict mode. Consider what would happen if you wanted the list of &lt;book&gt;s folded on the &lt;isbn&gt; values, <b>but</b> one of the &lt;isbn&gt;s was missing. Without strict mode, XML::Simple would issue a warning (which you may or may not see) and then leave the &lt;book&gt;s as an array rather than folding to a hash - which would most likely break whatever code followed. With strict mode, your script will die with a fatal error message when the bad data is encountered.</p> <p>As you can see, strict mode is not magic - it can't tell you when your code is wrong, but (like 'use strict') it can trap common causes of problems. An obvious question is "why not just enable this behaviour when 'use strict' is in force?". The problem of course is that most people are already including 'use strict' in their scripts (you are doing that right?) so upgrading to version 2.0 would appear to break many scripts. Instead, the developer can upgrade and then enable XML::Simple's strict mode one script at a time - fixing any problems as they are detected.</p> <p> <font size="-2">Edit: s/30/26/g - [dvergin] 2002-12-09</font></p>