Hello Wroof and welcome,
consider that there are many ways to do it in Perl...
Normally a Perl programmer after years of using open meet also his father sysopen to have more granularity on existing or not file.
>perl -e "use strict; use warnings;use Fcntl; my $fh; sysopen ($fh, 'f
+ilefilefile.log', O_WRONLY|O_APPEND ) or die $!"
No such file or directory at -e line 1.
>
>
>
>
>
>
>ls filefilefile.log
ls: filefilefile.log: No such file or directory
>
>
>perl -e "use strict; use warnings;use Fcntl; my $fh; sysopen ($fh, 'f
+ilefilefile.log', O_WRONLY|O_EXCL|O_CREAT ) or die
$!"
>
>
>
>
>ls filefilefile.log
filefilefile.log
>
>
>
------
Here are examples of open and sysopen in action.
To open for reading:
open(FH, "<", $path) or die $!;
sysopen(FH, $path, O_RDONLY) or die $!;
To open for writing, create a new file if needed, or else truncate an
+old one:
open(FH, ">", $path) or die $!;
sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT) or die $!;
sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT, 0600) or die $!;
To open for writing, create a new file, but that file must not previou
+sly exist:
sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT, 0600) or die $!;
To open for appending, creating it if necessary:
open(FH, ">>", $path) or die $!;
sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT) or die $!;
sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT, 0600) or die $!;
To open for appending, where the file must exist:
sysopen(FH, $path, O_WRONLY|O_APPEND) or die $!;
To open for update, where the file must exist:
open(FH, "+<", $path) or die $!;
sysopen(FH, $path, O_RDWR) or die $!;
To open for update, but create a new file if necessary:
sysopen(FH, $path, O_RDWR|O_CREAT) or die $!;
sysopen(FH, $path, O_RDWR|O_CREAT, 0600) or die $!;
To open for update, where the file must not exist:
sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT) or die $!;
sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT, 0600) or die $!;
We use a creation mask of 0600 here only to show how to create a priva
+te file. The argument is normally omitted.
HtH L*
UPDATE: some docs are missing
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|