sysopen FILEHANDLE,FILENAME,MODE,PERMSOpens the file whose filename is given by FILENAME, and associates it withFILEHANDLE. If FILEHANDLE is an expression, its value is used as the realfilehandle wanted; an undefined scalar will be suitably autovivified. Thisfunction calls the underlying operating system's open(2) function with theparameters FILENAME, MODE, and PERMS.
The possible values and flag bits of the MODE parameter aresystem-dependent; they are available via the standard module Fcntl
. Seethe documentation of your operating system's open(2) syscall to seewhich values and flag bits are available. You may combine several flagsusing the |
-operator.
Some of the most common values are O_RDONLY
for opening the file inread-only mode, O_WRONLY
for opening the file in write-only mode,and O_RDWR
for opening the file in read-write mode.
For historical reasons, some values work on almost every systemsupported by Perl: 0 means read-only, 1 means write-only, and 2means read/write. We know that these values do not work underOS/390 & VM/ESA Unix and on the Macintosh; you probably don't want touse them in new code.
If the file named by FILENAME does not exist and the open
call createsit (typically because MODE includes the O_CREAT
flag), then the value ofPERMS specifies the permissions of the newly created file. If you omitthe PERMS argument to sysopen
, Perl uses the octal value 0666
.These permission values need to be in octal, and are modified by yourprocess's current umask
.
In many systems the O_EXCL
flag is available for opening files inexclusive mode. This is not locking: exclusiveness means here thatif the file already exists, sysopen() fails. O_EXCL
may not workon network filesystems, and has no effect unless the O_CREAT
flagis set as well. Setting O_CREAT|O_EXCL
prevents the file frombeing opened if it is a symbolic link. It does not protect againstsymbolic links in the file's path.
Sometimes you may want to truncate an already-existing file. Thiscan be done using the O_TRUNC
flag. The behavior ofO_TRUNC
with O_RDONLY
is undefined.
You should seldom if ever use 0644
as argument to sysopen
, becausethat takes away the user's option to have a more permissive umask.Better to omit it. See the perlfunc(1) entry on umask
for moreon this.
Note that sysopen
depends on the fdopen() C library function.On many Unix systems, fdopen() is known to fail when file descriptorsexceed a certain value, typically 255. If you need more filedescriptors than that, consider rebuilding Perl to use the sfio
library, or perhaps using the POSIX::open() function.
See perlopentut for a kinder, gentler explanation of opening files.
Portability issues: sysopen in perlport.