in reply to Re^2: What is the point of a+ and w+ when opening files?
in thread What is the point of a+ and w+ when opening files?

Firstly, just to clear up a few things:

In https://perlmaven.com/open-to-read-and-write:

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.

— Ken