469,890 Members | 2,091 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Q: What's wrong with this?

Hi,

This is related to my previous post but I made it much simpler...

open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?
print$_ output gives me
one
two
one
two

but cat test.txt is still:
one
one

Thanks
Jul 19 '05 #1
11 2693
"Troll" <ab***@microsoft.com> tried to express:

open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?
print$_ output gives me
one
two
one
two

but cat test.txt is still:
one
one


because you are in no way writing to the file. You are simply reading
it and processing the data that you read. You would have to write the
changed data back out to a new file after it is processed like with:

perl myscript > test.txt.changed
Jul 19 '05 #2

"Wiseguy" <no***@celeron.local> wrote in message
news:3f********@127.0.0.1...
"Troll" <ab***@microsoft.com> tried to express:

open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?
print$_ output gives me
one
two
one
two

but cat test.txt is still:
one
one


because you are in no way writing to the file. You are simply reading
it and processing the data that you read. You would have to write the
changed data back out to a new file after it is processed like with:

perl myscript > test.txt.changed


What if....
I have corrected my opener to accept writing to the file:
open (INFILE, "+<test.txt");
while (<>) {
s/one/two/g;
print INFILE $_;
}

but still it's playing up.

If the contents of test.txt are:
one

then the new file test.txt becomes:
one
one
two

Any hints? I'm going round in circles it seems...


Jul 19 '05 #3
Troll wrote:
open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?


Why should it be changed?
You are not writing to the file anywhere in your code, so of course the file
on the hard drive remains unchanged.
It would be a terrible thing if Perl would alter the content of a file while
reading it.

Maybe you are looking for the answer of PerlFAQ5:
"How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?"

jue
Jul 19 '05 #4
Troll wrote:
"Wiseguy" <no***@celeron.local> wrote in message
news:3f********@127.0.0.1...
"Troll" <ab***@microsoft.com> tried to express: I have corrected my opener to accept writing to the file:
open (INFILE, "+<test.txt");


You should have read the docs, too. About using "+" it says (among other
things):
[...] You can't usually use either
read-write mode for updating textfiles, since they have variable
length records.
while (<>) {
s/one/two/g;
print INFILE $_;
}

but still it's playing up.

If the contents of test.txt are:
one

then the new file test.txt becomes:
one
one
two
I am surprised that you are getting any meaningful results at all.
This random mix of reading and writing without any consious repositioning of
the file pointer can only result in chaos eventually.
Any hints? I'm going round in circles it seems...


Yes, Read The Fine Manual.
And maybe get some basic understanding about how files work. This has
nothing to do with Perl but is general programming 101.

jue
Jul 19 '05 #5

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:vS*****************@nwrddc03.gnilink.net...
Troll wrote:
"Wiseguy" <no***@celeron.local> wrote in message
news:3f********@127.0.0.1...
"Troll" <ab***@microsoft.com> tried to express: I have corrected my opener to accept writing to the file:
open (INFILE, "+<test.txt");


You should have read the docs, too. About using "+" it says (among other
things):
[...] You can't usually use either
read-write mode for updating textfiles, since they have

variable length records.
while (<>) {
s/one/two/g;
print INFILE $_;
}

but still it's playing up.

If the contents of test.txt are:
one

then the new file test.txt becomes:
one
one
two
I am surprised that you are getting any meaningful results at all.
This random mix of reading and writing without any consious repositioning

of the file pointer can only result in chaos eventually.
Any hints? I'm going round in circles it seems...


Yes, Read The Fine Manual.
And maybe get some basic understanding about how files work. This has
nothing to do with Perl but is general programming 101.

jue


Thanks. I guess I'm not the only one surprised. I'd love to read the manual
in depth but have no time atm. :(
That's why I'm posting here so much. Is there a really silly reason for the
script failure [other that not having read the manual]?

Also, which FAQ did u refer to? http://www.faqs.org/faqs/perl-faq/ ? If so,
I could not find what I wanted in part5. Or did I miss it?

Jul 19 '05 #6
Troll wrote:
Thanks. I guess I'm not the only one surprised. I'd love to read the
manual in depth but have no time atm. :(
That's why I'm posting here so much. Is there a really silly reason
for the script failure [other that not having read the manual]?
Listen, you are using the wrong tool!
It's like you are trying to use screwdriver to hammer a nail into a wall.
The docs explain that that is not a good idea and that you should use a
hammer instead.

Side note: yes, you can read and write to the same file at the same time,
but it requires MUCH(!) more knowledge, work, and diligence, and it works
completely different then you seem to believe.
Also, which FAQ did u refer to? http://www.faqs.org/faqs/perl-faq/ ?
If so, I could not find what I wanted in part5. Or did I miss it?


I don't know who this guy U is, but I was refering to "perldoc -q middle"
which will take you right to the relevant answer.

jue
Jul 19 '05 #7

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:YA*****************@nwrddc02.gnilink.net...
Troll wrote:
Thanks. I guess I'm not the only one surprised. I'd love to read the
manual in depth but have no time atm. :(
That's why I'm posting here so much. Is there a really silly reason
for the script failure [other that not having read the manual]?


Listen, you are using the wrong tool!
It's like you are trying to use screwdriver to hammer a nail into a wall.
The docs explain that that is not a good idea and that you should use a
hammer instead.

Side note: yes, you can read and write to the same file at the same time,
but it requires MUCH(!) more knowledge, work, and diligence, and it works
completely different then you seem to believe.
Also, which FAQ did u refer to? http://www.faqs.org/faqs/perl-faq/ ?
If so, I could not find what I wanted in part5. Or did I miss it?


I don't know who this guy U is, but I was refering to "perldoc -q middle"
which will take you right to the relevant answer.

jue


Sorry Jue. I just got into perldoc. Do I enter the whole "perldoc -q middle"
into the search field or just a part of it? Not sure what the -q means.
Never been there b4...
Jul 19 '05 #8
Troll wrote:
Sorry Jue. I just got into perldoc. Do I enter the whole "perldoc -q
middle"
Yes (well, without the quotes)
into the search field or just a part of it?
Search field? What search field? You enter it at the command line.
Not sure what the
-q means. Never been there b4...


Reply from "perldoc" when called without any arguments:
The -h option prints more help. Also try "perldoc perldoc" to get
acquainted with the system.

And from "perldoc perldoc":
[...]
OPTIONS
-h help
Prints out a brief help message.
[...]
-q perlfaq
The -q option takes a regular expression as an argument. It will
search the question headings in perlfaq[1-9] and print the entries
matching the regular expression.

jue
Jul 19 '05 #9

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:Mi*****************@nwrddc02.gnilink.net...
Troll wrote:
Sorry Jue. I just got into perldoc. Do I enter the whole "perldoc -q
middle"
Yes (well, without the quotes)
into the search field or just a part of it?


Search field? What search field? You enter it at the command line.
Not sure what the
-q means. Never been there b4...


Reply from "perldoc" when called without any arguments:
The -h option prints more help. Also try "perldoc perldoc" to get
acquainted with the system.

And from "perldoc perldoc":
[...]
OPTIONS
-h help
Prints out a brief help message.
[...]
-q perlfaq
The -q option takes a regular expression as an argument. It will
search the question headings in perlfaq[1-9] and print the

entries matching the regular expression.

jue


Ohhhh. I was looking at perldoc online. Now, why would any1 do that if it
can be run from the command line? Don't ask me. It was just a natural
reaction.

The only drama I have is that Perl 5.8.0 is not available to me so I cannot
use the Tie::File module.
As such I'm trying to go with the solution proposed in
http://www.perldoc.com/perl5.005_03/...ing-of-a-file-

but I'm getting the following errors:
Global symbol $file requies explicit package name at ./
Global symbol $old requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $new requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $bak requies explicit package name at ./
etc
My script begins with:
******************
....
use strict;
$file = test.html;

$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";
etc


Jul 19 '05 #10
Troll wrote:
The only drama I have is that Perl 5.8.0 is not available to me so I
Why not? You can get it for free from e.g. CPAN
cannot use the Tie::File module.
I am not aware that Tie::File would require 5.8, but I may be wrong.
As such I'm trying to go with the solution proposed in
http://www.perldoc.com/perl5.005_03/...ing-of-a-file-
but I'm getting the following errors:
Global symbol $file requies explicit package name at ./
Global symbol $old requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $new requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $bak requies explicit package name at ./
etc
My script begins with:
******************
...
use strict;
$file = test.html;
Probably you meant
my $file = 'test.html';
$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";


Probably you meant
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";

jue
Jul 19 '05 #11

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:HP****************@nwrddc02.gnilink.net...
Troll wrote:
The only drama I have is that Perl 5.8.0 is not available to me so I


Why not? You can get it for free from e.g. CPAN
cannot use the Tie::File module.


I am not aware that Tie::File would require 5.8, but I may be wrong.
As such I'm trying to go with the solution proposed in

http://www.perldoc.com/perl5.005_03/...ing-of-a-file-

but I'm getting the following errors:
Global symbol $file requies explicit package name at ./
Global symbol $old requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $new requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $bak requies explicit package name at ./
etc
My script begins with:
******************
...
use strict;
$file = test.html;


Probably you meant
my $file = 'test.html';
$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";


Probably you meant
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";

jue


Actually yes. Thanks. I got the same result by initialising the variables
but the 'my' option is better.
I'm unable to use the latest Perl distro as the sysadmins have not updated
to it. I forgot which ver they are running but it sure ain't one of the
recent ones.

With your guidance, I finally got the result I wanted - now it's just a case
of putting my own RegExes in there. Someone had suggested that I use the
"+<test.txt" and that's how the whole problem started. I was running around
like a headless chook not really knowing what I'm looking for.

Thanks very much for sticking with me on this one. Greatly appreciated :c)

Jul 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Henley | last post: by
72 posts views Thread by E. Robert Tisdale | last post: by
121 posts views Thread by typingcat | last post: by
28 posts views Thread by Madhur | last post: by
9 posts views Thread by Pyenos | last post: by
3 posts views Thread by Siong.Ong | last post: by
89 posts views Thread by Tubular Technician | last post: by
20 posts views Thread by Daniel.C | last post: by
24 posts views Thread by MU | last post: by
2 posts views Thread by mingke | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.