1

I am creating $input with this code: push(@{$input->{$step}},$time);, then I save it in an xml file, and at the next compiling, I read it from that file. When i print it, i get the structure bellow.

if(-e $file)
my $input =XMLin($file...);

print Dumper $input;

and I get this structure

$VAR1 = {
      'opt' => {
               'step820' => '0',
               'step190' => '0',
               'step124' => '0',
               }
    };

for each step with it's time..

push(@{$input->{$step}},$time3);


XmlOut($file, $input);

If I run the program again, I get this structure:

$VAR1 = {
      'opt' => {
               'step820' => '0',
               'step190' => '0',
               'step124' => '0',
               'opt' => {
                        'step820' => '0',
                        'step190' => '0',
                        'step124' => '0'
                        }
         }

I just need to overwrite the values of steps(ex:$var1->opt->step820 = 2). How can i do that?

user30771
  • 109
  • 6
  • 1
    Start with not using XML::Simple, which is horrible. And post some sample XML with a desired output. – Sobrique Feb 15 '16 at 14:06
  • 2
    The XML::Simple is requested.. i know there are other methods – user30771 Feb 15 '16 at 14:10
  • The desired output would be the same with the first Dumper output.. `$VAR1 = { 'opt' => { 'step820' => '0', 'step190' => '0', 'step124' => '0', } }; ` what i am trying to do is to acces the values and modify them – user30771 Feb 15 '16 at 14:12
  • 1
    [Why is XML::Simple “Discouraged”?](http://stackoverflow.com/questions/33267765/why-is-xmlsimple-discouraged) – Sobrique Feb 15 '16 at 14:20

3 Answers3

1

I just need to overwrite the values of steps(ex:$var1->opt->step820 = 2). How can i do that?

$input->{opt}->{step820} = 2;
Georg Mavridis
  • 2,312
  • 1
  • 15
  • 23
1

I'm going to say what I always do, whenever someone posts something asking about XML::Simple - and that is that XML::Simple is deceitful - it isn't simple at all.

Why is XML::Simple "Discouraged"?

So - in your example:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $xml= XML::Twig->new->parsefile($file);
$xml -> get_xpath('./opt/step820',0)->set_text("2");
$xml -> print;

The problem is that XML::Simple is only any good for parsing the type of XML that you didn't really need XML for in the first place.

For more simple examples - have you considered using JSON for serialisation? As it more directly reflects the hash/array structure of native perl data types.

That way you can instead:

print {$output_fh} to_json ( $myconfig, {pretty=>1} ); 

And read it back in:

my $myconfig = from_json ( do { local $/; <$input_fh> }); 

Something like:

#!/usr/bin/env perl
use strict;
use warnings;

use JSON; 

my $input; 

my $time = 0; 
foreach my $step ( qw ( step820 step190 step124 ) ) {
    push(@{$input->{$step}},$time);
}

print to_json ( $input, {pretty=>1} );

Giving resultant JSON of:

{
   "step190" : [
      0
   ],
   "step820" : [
      0
   ],
   "step124" : [
      0
   ]
}

Although actually, I'd probably:

foreach my $step ( qw ( step820 step190 step124 ) ) {
    $input->{$step} = $time;
}

print to_json ( $input, {pretty=>1} );

Which gives;

{
   "step190" : 0,
   "step124" : 0,
   "step820" : 0
}

JSON uses very similar conventions to perl - in that {} denote key value pairs (hashes) and [] denote arrays.

Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • The XML::Simple is requested, because it is "basic" (at least that's what I've benn told) – user30771 Feb 15 '16 at 15:19
  • 1
    It is basic, in the same way as trying to light a fire by twiddling a piece of wood. E.g. it's hard work, and prone to failure, and by the way wouldn't you rather use the lighter you've got in your pocket? – Sobrique Feb 15 '16 at 15:45
0

Look at the RootName option of XMLout. By default, when "XMLout()" generates XML, the root element will be named 'opt'. This option allows you to specify an alternative name.

Specifying either undef or the empty string for the RootName option will produce XML with no root elements.

neuhaus
  • 3,886
  • 1
  • 10
  • 27