I want to peform multiple writes to a file, from different processes, at the same time. Writes will be at different, non-overlapping positions. I want to use perlio for this (to avoid coding workarounds when syswrite is not writing all data). And the file to write data, might not exsit before write.

So, I found this is pretty complicated. Perl open modes maps to fopen() fopen

And it does not have mode with O_CREAT, but without O_TRUNC/O_APPEND

Obviously I cannot use O_TRUNC because it will destroy file content, and cannot use O_APPEND because I cannot use seek with it.

possible workaround is only use

open my $f, "+>>", $filename; close $f; open $f, '+<';

This will create file if it's not exists, and will open it in read-write mode. (I assume there will be no race-conditions between close and second open, such race condition can be worked around if I check errno after last open and put it into while loop)

Question: Am I missing something? It there easier way? Or maybe better avoid perl IO for such task (i.e. above code contain bugs) ?

In reply to PerlIO, random write file access and O_CREAT by vsespb

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.