D
dshan
Hi all!
I'm a Perl newbie. My question is: how do I insert data contained in
$string at a certain position in a file? I tried to use
open(OUT,"+< $out") or die "Can't read/write $out: $!";
while(<OUT>) {
if($_ =~ /<add_here \/>/s) { print OUT $string; }
}
to insert new data into an XML file which looks like this:
<?xml version="1.0" encoding="Windows-1251"?>
<disc>
<info>Some Info</info>
<info>Some Info 2</info>
....
<add />
</disc>
but on update it prints the following:
<?xml version="1.0" encoding="Windows-1251"?>
<disc>
<info>Some Info</info>
<info>Some Info 2</info>
....
<add_here />
</disc>
<?xml version="1.0" encoding="Windows-1251"?>
<disc>
<info>Some Info 1</info>
<info>Some Info 2</info>
....
<add_here />
</disc>
<info>Some Info 3</info>
that is, it appends the preceding data and the new data ($string) at
the end of the file. Why? I just want to add new text at the <add_here>
position...
Regards,
dshan
I'm a Perl newbie. My question is: how do I insert data contained in
$string at a certain position in a file? I tried to use
open(OUT,"+< $out") or die "Can't read/write $out: $!";
while(<OUT>) {
if($_ =~ /<add_here \/>/s) { print OUT $string; }
}
to insert new data into an XML file which looks like this:
<?xml version="1.0" encoding="Windows-1251"?>
<disc>
<info>Some Info</info>
<info>Some Info 2</info>
....
<add />
</disc>
but on update it prints the following:
<?xml version="1.0" encoding="Windows-1251"?>
<disc>
<info>Some Info</info>
<info>Some Info 2</info>
....
<add_here />
</disc>
<?xml version="1.0" encoding="Windows-1251"?>
<disc>
<info>Some Info 1</info>
<info>Some Info 2</info>
....
<add_here />
</disc>
<info>Some Info 3</info>
that is, it appends the preceding data and the new data ($string) at
the end of the file. Why? I just want to add new text at the <add_here>
position...
Regards,
dshan