Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open a file for writing only when it doesn't exist in Perl 6?

Tags:

raku

According to the open docs, there are adverbs for reading, writing, and appending. That's fine and what I would expect. I have a particular application that uses sysopen for better control and I was trying to rewrite it in Perl 6. I know about NativeCall (as mentioned in my question about kill), but is there something builtin that I'm missing?

like image 811
brian d foy Avatar asked Apr 14 '17 01:04

brian d foy


1 Answers

This is a case of incomplete documentation:

On MoarVM, open has supported the more common ones of the POSIX flags since 2015, including O_EXCL via the named parameter :exclusive.

The flag combination you're looking for is

my $fh = open "file", :mode<wo>, :create, :exclusive;

which can be written more compactly as

my $fh = open "file", :x;

This will hopefully be documented as part of the ongoing grant Standardization, Test Coverage, and Documentation of Perl 6 I/O Routines. For now, details can be found in the commit log. There have been some minor changes since then; in particular, :mode<pipe> has been removed and a JVM implemetation added (which however does not allow you to combine flags as freely as MoarVM does).

like image 104
Christoph Avatar answered Jan 01 '23 19:01

Christoph