Q: What's wrong with this?

T

Troll

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
 
W

Wiseguy

Troll said:
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
 
T

Troll

Wiseguy said:
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...
 
J

Jürgen Exner

Troll said:
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
 
J

Jürgen Exner

Troll said:
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
 
T

Troll

Jürgen Exner said:
Troll said:
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?
 
J

Jürgen Exner

Troll said:
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
 
T

Troll

Jürgen Exner said:
Troll said:
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...
 
J

Jürgen Exner

Troll said:
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
 
T

Troll

Jürgen Exner said:
Troll said:
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...-of-a-file-append-to-the-beginning-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
 
J

Jürgen Exner

Troll said:
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...-of-a-file-append-to-the-beginning-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
 
T

Troll

Jürgen Exner said:
Why not? You can get it for free from e.g. CPAN


I am not aware that Tie::File would require 5.8, but I may be wrong.

http://www.perldoc.com/perl5.005_03...-of-a-file-append-to-the-beginning-of-a-file-

Probably you meant
my $file = 'test.html';


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)
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top