substitution in open file

J

jtbutlerhvb

I am trying to append data in an XML file but first I want to remove
the final tag. I am having a problem removing the last line. It ends
up clearing the entire file. Anyone give me push in the right
direction as to how I can get rid of a certain string in an open file?

open(DAT, ">menu.xml");

while(<DAT>)
{
s/<\/menu>/ / ;
}
 
P

Paul Lalli

I am trying to append data in an XML file but first I want to remove
the final tag. I am having a problem removing the last line. It ends
up clearing the entire file. Anyone give me push in the right
direction as to how I can get rid of a certain string in an open file?

open(DAT, ">menu.xml");

You just opened the file for writing, thus clearing out the entire
file. Change to:
open my $DAT, '<', 'menu.xml' or die "Cannot open 'menu.xml' for
reading: $!\n";

See also:
perldoc -f open
perldoc perlopentut

Paul Lalli
 
J

jtbutlerhvb

This doesnt do anything now. Is my substitution method wrong?

open my $DAT, '<', 'menu.xml';

while(<DAT>)
{
s/<\/menu>/ / ;
}

close(DAT);
 
E

Eden Cardim

(e-mail address removed) escreveu:
This doesnt do anything now. Is my substitution method wrong?

open my $DAT, '<', 'menu.xml';

while(<DAT>)
{
s/<\/menu>/ / ;
}

close(DAT);

You need to write the data back to your file. So, either you need to
store the entire file in memory, or use a temporary file. The second
option is best IMHO since you should always back up files that are
modified automatically, in case anything goes wrong, here's how I would
do it:

my $file = 'menu.xml';
`cp $file $file~`;
open DAT "<$file~";
open ALTERED ">$file";
while(<DAT>) {
s/<\/menu>//;
print ALTERED $_;
}
close DAT;
close ALTERED;
 
J

jtbutlerhvb

That makes sense. Thanks a lot this helps me - I can modify it now to
do what I need.
 
P

Paul Lalli

Eden said:
You need to write the data back to your file. So, either you need to
store the entire file in memory, or use a temporary file. The second
option is best IMHO since you should always back up files that are
modified automatically, in case anything goes wrong, here's how I would
do it:

my $file = 'menu.xml';
`cp $file $file~`;
open DAT "<$file~";
open ALTERED ">$file";
while(<DAT>) {
s/<\/menu>//;
print ALTERED $_;
}
close DAT;
close ALTERED;

Alternative, using Perl's in-place editing feature, rather than
shelling out to `cp`:

{
local @ARGV = 'menu.xml';
local $^I = '~';
while (<>){
s{</menu>}{};
} continue {
print;
}
}

read more about $^I in
perldoc perlrun
and
perldoc perlvar

Paul Lalli
 
T

Tad McClellan

This doesnt do anything now.


The program does not make any output.

It will appear to do nothing if you do not have it make some output.

Is my substitution method wrong?

No.


open my $DAT, '<', 'menu.xml'; ^^^^
^^^^
while(<DAT>)
^^^
^^^

One of these things is not like the other, one of these things
just doesn't belong...

You should always enable warnings when developing Perl code!

If you had, perl would have found that mistake *for you*, but
you have to ask for it, so ask.
 
S

Sherm Pendley

Eden Cardim said:
One more perlvar I wont forget about... (it's hard to remember them all)

Why try to remember them all? That's what reference books are for. So long
as you know there's a perlvar that has the effect you need, you can always
look it up if you forget exactly which one it is.

sherm--
 
E

Eden Cardim

Why try to remember them all? That's what reference books are for. So long
as you know there's a perlvar that has the effect you need, you can always
look it up if you forget exactly which one it is.

Well, knowing there's a perlvar that has the effect I need involves
remembering *something*... The vars we know by heart are typically the
ones we use the most, and the ones that cause the greatest impact once
we use them for the first time, it's like riding a bike.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top