Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Checking if a variable is an array

by ferreira (Chaplain)
on Jun 28, 2007 at 13:35 UTC ( #623886=note: print w/replies, xml ) Need Help??


in reply to Checking if a variable is an array

That's a feature of XML::Simple - this difference of behavior between a node which happens once or multiple times. Most of the time, when you expect a node that happen one or more times, you want to tell XML::Simple to always return an array of hashes, so you have a uniform way to deal with the data.

That's what ForceArray attribute for.

$data = XMLin( $xml, ForceArray => [ qw(Layer) ] )
will guarantee that every time Layer is seen, it creates an array (ref).

Otherwise you'll always have to code something like that:

my $layers = $data->{Capabilities}->{Layer}; for my $layer ( ref $layers eq 'ARRAY' ? @$layers : ( $layers ) ) { # do something to each Layer }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://623886]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2020-10-19 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (205 votes). Check out past polls.

    Notices?