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


In reply to Re^3: What is the point of a+ and w+ when opening files? by kcott
in thread What is the point of a+ and w+ when opening files? by Maelstrom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.