Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Check if a file exists and if not create it - use sysopen

by Discipulus (Canon)
on Aug 21, 2014 at 11:56 UTC ( [id://1098225]=note: print w/replies, xml ) Need Help??


in reply to Check if a file exists and if not create it

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.

Replies are listed 'Best First'.
Re^2: Check if a file exists and if not create it - use sysopen
by t-rex (Scribe) on Jun 24, 2016 at 08:34 UTC

    i get bare word error when i complie the script using flags O_RDWR or others, what could be the reason ?

      have you imported the module with use Fcntl ? see Fcntl

      L*

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1098225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found