Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Yet another xml::simple question

by sandcrawler (Initiate)
on Jan 26, 2009 at 21:29 UTC ( #739039=perlquestion: print w/replies, xml ) Need Help??

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

Good day Monks! I guess work's just got my head so clouded now that I can't read between the lines of what the tutorials are trying to tell me so forgive me if this is a dumb question.

I'm using XML::Simple 2.18 with Perl 5.8.8.

I'm trying to read a conf file, perform some stuff, and then in the conf file keep track of the last time I executed my code.

My XML looks like
<conf> <box1> <username>me</username> <password>secret</password> </box1> <box2> <username>me</username> <password>secret</password> </box2> <lastRun>DATE</lastRun> </conf>
DATE above should be epoch time. my simplified perl is
#!/usr/bin/perl use strict; use XML::Simple; use Data::Dumper; my $xmlConf = "file.xml"; my $conf = new XML::Simple(NoAttr => 1, KeyAttr => [], ForceArray => [ +qw(box1 box2)], KeepRoot => 1, SearchPath => "." ); my $confIn = $conf->XMLin("$xmlConf"); my $confOut = $conf->XMLout($confIn, OutputFile => $xmlConf); print "Box 1: Username - $confIn->{conf}->{Box1}[0]->{username}, Passw +ord - $confIn->{conf}->{Box1}[0]->{password}\n"; print "Box 2: Username - $confIn->{conf}->{Box2}[0]->{username}, Passw +ord - $confIn->{conf}->{Box2}[0]->{password}\n"; my $epoch = time(); $confOut->{conf}->{lastRun} = $epoch;
My code runs fine up till I try to write out the update. I get
Can't use string ("1") as a HASH ref while "strict refs" in use at line foo

Thanks in advance for the forthcoming enlightenment!


Replies are listed 'Best First'.
Re: Yet another xml::simple question
by kyle (Abbot) on Jan 26, 2009 at 21:37 UTC

    You have this:

    my $confOut = $conf->XMLout($confIn, OutputFile => $xmlConf); # ... $confOut->{conf}->{lastRun} = $epoch;

    I don't use XML::Simple much, but I'm guessing that XMLout returns a simple status code (1 for success), and you're trying to use that has a hash reference.

    I'm guessing here somewhat since the error you show us says "line foo", and there's no indication of which line that is.

    Update: After a little fooling around, I find that this does what I think you want:

      The line it references is the line with
      $confOut->{conf}->{lastRun} = $epoch;
      on it.
      the resulting XML does not have the new epoch time it in.
      It does seem I need to go back and look at the tutorial I used as the basis of my code

      I was just fixing to update with nearly the exact same thing. Your first response was the nudge I needed. Thanks for the help!!

        Yet another recommendation to use XML::Twig instead, if you're on a learning curve anyway.
Re: Yet another xml::simple question
by Anonymous Monk on Jan 26, 2009 at 23:08 UTC
    I would re-read the documentation on XML::Simple e.g. documentation on the ForceArray parameter. In the past, I've found using Data::Dumper useful in seeing the data structure. That way, you'll know what the type of $confOut->{conf} is. Clearly, it isn't a hashref. Again, this can all be found in XML::Simple's documentation.
Re: Yet another xml::simple question
by saberworks (Curate) on Jan 27, 2009 at 18:03 UTC
    Just to clarify, you're using XMLOut wrong. XMLOut takes a data structure and turns it into XML (a string). From the docs:
    The default behaviour of XMLout() is to return the XML as a string. If you wish to write the XML to a file, simply supply the filename using the 'OutputFile' option.
    So with the options you're using to XMLOut, you are telling it to write the XML file right away. But then after that, you're trying to mess with the data structure. What you need to do is build up your data structure first (including the time stamp there) and only then call XMLOut.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://739039]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-04-18 06:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found