Firstly, just to clear up a few things:
-
I thought you were comparing with another language because you wrote
w+ instead of +> and a+ instead of +>>.
-
I didn't think you were looking for a problem solution; more of an explanation.
-
I didn't say you were disagreeing with anything; I said that the documentation was agreeing with you.
-
Perl is the language; perl is the program that runs Perl code; PERL is not a thing. :-)
In https://perlmaven.com/open-to-read-and-write:
-
The file is opened: open my $fh, '+<', $file — position is 0.
-
The first record is read: <$fh> — position is >0.
-
The filehandle is repositioned to the start: seek $fh, 0, 0 — position is 0.
-
The file contents are removed: truncate $fh, 0 — position is 0.
-
New content is added: print $fh $count — position is >0.
The only reason that seek() was used here was to reposition to the start.
Note that truncate() does not change the position.
There is no inherent requirement to use seek() or truncate()
after opening a file in one of the read/write modes.
The die() message with "mailbox" is an error; probably a copy-paste typo.
Hand-crafting I/O messages is tedious and error-prone.
I prefer to leave that task to Perl with the autodie pragma;
it's less work for me and it won't produce bogus or incomplete messages;
I recommend that you use this too.
I threw together a short script (read_write_append.pl) to demonstrate the three read/write modes:
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use autodie;
{
say "\n*** Test +< (r+)";
say 'Open file';
open my $fh, '+<', 'test1.txt';
say 'Position in file: ', tell $fh;
say 'Read to end of file';
print while <$fh>;
say 'Position in file: ', tell $fh;
}
{
say "\n*** Test +> (w+)";
say 'Open file';
open my $fh, '+>', 'for_del.txt';
say 'Position in file: ', tell $fh;
say 'Read to end of file';
print while <$fh>;
say 'Position in file: ', tell $fh;
}
{
say "\n*** Test +>> (a+)";
say 'Open file';
open my $fh, '+>>', 'testq.txt';
say 'Position in file: ', tell $fh;
say 'Read to end of file';
print while <$fh>;
say 'Position in file: ', tell $fh;
say 'Write a record';
say $fh 'r';
say 'Position in file: ', tell $fh;
say 'Rewind to start of file';
seek $fh, 0, 0;
say 'Position in file: ', tell $fh;
say 'Read to end of file';
print while <$fh>;
say 'Position in file: ', tell $fh;
}
I created some test files:
ken@titan ~/tmp/pm_11152251_file_rwa
$ ls -l *.txt
-rw-r--r-- 1 ken None 6 May 19 01:41 for_del.txt
-rw-r--r-- 1 ken None 6 May 19 01:16 test1.txt
-rw-r--r-- 1 ken None 6 May 19 01:40 testq.txt
ken@titan ~/tmp/pm_11152251_file_rwa
$ cat test1.txt
1
2
3
ken@titan ~/tmp/pm_11152251_file_rwa
$ cat for_del.txt
z
x
c
ken@titan ~/tmp/pm_11152251_file_rwa
$ cat testq.txt
q
w
e
ken@titan ~/tmp/pm_11152251_file_rwa
$
Ran the script:
ken@titan ~/tmp/pm_11152251_file_rwa
$ ./read_write_append.pl
*** Test +< (r+)
Open file
Position in file: 0
Read to end of file
1
2
3
Position in file: 6
*** Test +> (w+)
Open file
Position in file: 0
Read to end of file
Position in file: 0
*** Test +>> (a+)
Open file
Position in file: 6
Read to end of file
Position in file: 6
Write a record
Position in file: 8
Rewind to start of file
Position in file: 0
Read to end of file
q
w
e
r
Position in file: 8
ken@titan ~/tmp/pm_11152251_file_rwa
$
Then checked the files again:
ken@titan ~/tmp/pm_11152251_file_rwa
$ ls -l *.txt
-rw-r--r-- 1 ken None 0 May 19 01:42 for_del.txt
-rw-r--r-- 1 ken None 6 May 19 01:16 test1.txt
-rw-r--r-- 1 ken None 8 May 19 01:42 testq.txt
ken@titan ~/tmp/pm_11152251_file_rwa
$ cat test1.txt
1
2
3
ken@titan ~/tmp/pm_11152251_file_rwa
$ cat for_del.txt
ken@titan ~/tmp/pm_11152251_file_rwa
$ cat testq.txt
q
w
e
r
ken@titan ~/tmp/pm_11152251_file_rwa
$
I suggest that you play around with that code to test whatever other scenarios might interest you.
|