P
Paul Lalli
Greetings.
I am attempting to understand what is the use of opening files for both
reading and writing. I have read both
perldoc -f open
and
perldoc perlopentut
but I am confused. The above documentation refers to using '+<' mode as
'updating', and suggests -i is a better alternative. That's all well
and good, but I would still like to understand what specifying a mode of
'+<' actually *does*.
For example, a sample input file contains the five lines:
1
2
3
4
5
Executing the following script:
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '+<', 'input.txt' or die "Cannot open: $!\n";
my $line = <$fh>;
print "Line is: $line";
print $fh "A\n";
__END__
results in output of:
Line is: 1
and the input file now contains:
1
2
3
4
5
1
A
Can someone please explain this result to me? It looks as though perl
appended the file with whatever data I had already read, followed by the
output I actually printed. Is this what is supposed to happen? Is
there a way (using this method of opening a file for simultaneous read &
writes) to just print new data to the file, without printing copies of
any data that's already been read?
Thank you for your time,
Paul Lalli
I am attempting to understand what is the use of opening files for both
reading and writing. I have read both
perldoc -f open
and
perldoc perlopentut
but I am confused. The above documentation refers to using '+<' mode as
'updating', and suggests -i is a better alternative. That's all well
and good, but I would still like to understand what specifying a mode of
'+<' actually *does*.
For example, a sample input file contains the five lines:
1
2
3
4
5
Executing the following script:
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '+<', 'input.txt' or die "Cannot open: $!\n";
my $line = <$fh>;
print "Line is: $line";
print $fh "A\n";
__END__
results in output of:
Line is: 1
and the input file now contains:
1
2
3
4
5
1
A
Can someone please explain this result to me? It looks as though perl
appended the file with whatever data I had already read, followed by the
output I actually printed. Is this what is supposed to happen? Is
there a way (using this method of opening a file for simultaneous read &
writes) to just print new data to the file, without printing copies of
any data that's already been read?
Thank you for your time,
Paul Lalli