Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

XML::Simple forcearray problem

by csuhockey3 (Curate)
on Aug 07, 2003 at 19:22 UTC ( #282001=perlquestion: print w/replies, xml ) Need Help??
csuhockey3 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

'simple question' -- I am trying to use XML::Simple to gun through some XML but I am having a problem. When I attempt to run the script, I get the error 'No value specified for 'ForceArray' option in call to XMLin()...'

I have gone through the documentation paying close attention to forcearray, but it doesn't seem to click. What am I missing? I have tried multiple values and combinations to deal with forcearray and XMLin(). Anyone have a brief explanation or said a different way from the documentation? Thanks for the help as always.

snippet with the problem:

use Data::Dumper; use XML::Simple; #... open(FILE, "someFile.DTD") or die "can't open file: $!"; #... my $config = XMLin($FILE, forcearray => 1); print Dumper($config);

Replies are listed 'Best First'.
Re: XML::Simple forcearray problem
by erasei (Pilgrim) on Aug 07, 2003 at 20:08 UTC
    the 'XMLin' function doesn't take a file handle, but rather the file name as argument. From the perldoc:
    my $ref = XMLin([xml file or string>], [, <options>]);
    Try this instead:
    my $config = XMLin("someFile.DTD", forcearray => 1);
    In your code, you XMLin $FILE, but you don't declare $FILE in your code submitted. What I expect is happening is that you are trying to XMLin '$FILE' (the undeclared variable) thinking it is 'FILE' (the file handle). By XMLin'ing '$FILE' (which is empty) you are calling XMLin without all of the required arguments, thus throwing the error message mentioned.
Re: XML::Simple forcearray problem
by larsen (Parson) on Aug 07, 2003 at 19:38 UTC
    Are you sure the snippet you provide is the actual code giving the problem? Here an excerpt from XML::Simple:
    if($StrictMode and $dirn eq 'in') { croak "No value specified for 'ForceArray' option in call to XML +$dirn()"; } $opt->{forcearray} = 0;
    And $StrictMode is true only if you use XML::Simple in this way: use XML::Simple qw(:strict);

    Another issue is in the file-reading part:
    open(FILE, "someFile.DTD") or die "can't open file: $!"; #... my $config = XMLin($FILE, forcearray => 1);
    This use of FILE and $FILE isn't going to work.
Re: XML::Simple forcearray problem
by snax (Hermit) on Aug 07, 2003 at 19:34 UTC
    I don't have it installed, so I can't be certain, but according to the docs the option you want is ForceArray and not forcearray -- it is case sensitive.
Re: XML::Simple forcearray problem
by grantm (Parson) on Aug 09, 2003 at 21:05 UTC

    On points raised:

    • Option names used to be all lower case but since version 2.04 they are CaseInsensitive and can have embedded_underscores.
    • As larsen pointed out, you should only see that message if you have XML::Simple's strict mode enabled. If you're getting it with the code as you reported it's a bug - email me the failing code and I'll fix it.
    • XMLin() can accept a file handle, either some sort of IO::Handle object or a globref as in XMLin(\*DATA).
      Hi Grant,

      Sorry for the lag time, I have been ill. There is no bug to worry about, strict is indeed enabled. I tried to find some more answers in your XML FAQ (very nice by the way). It was informative, but I am still stranded.

      I have tried all of the above suggestions, but still seem to be having difficulty. I have never been stuck on someting like this before. It can't be that difficult. please remember to be gentle, I am very new at perl. Thanks again, below is what I have, complete (I took the rest of the code out, leaving only the problem). Any more ideas as to what I am doing wrong?

      #... use Data::Dumper; use XML::Simple; use XML::Simple qw(:strict); open(FILE, "someFile.DTD") or die "can't open file: $!"; my $config = XMLin("someFile.DTD", forcearray => 1); print Dumper($config);
        From the XML::Simple docs on importing ':strict':
        the following common mistakes will be detected and treated as fatal errors: Failing to explicitly set the KeyAttr option - if you can't be bothered reading about this option, turn it off with: KeyAttr => [] ...

        It's not the forcearray that's giving you the problem, its the lack of KeyAttr.

        Also note that you don't need two 'use XML::Simple' lines. One is plenty.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2018-08-16 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (166 votes). Check out past polls.

    Notices?