Home > php教程 > PHP开发 > Classic usage of Perl: Use Sysopen() for more control

Classic usage of Perl: Use Sysopen() for more control

黄舟
Release: 2016-12-16 13:52:52
Original
2096 people have browsed it

In order to better control how files are opened, you can use the sysopen() function:

useFcntl;

sysopen(FH,$filename,O_RDWR|O_CREAT,0666)

ordie"Can'topen$filenameforreading/writing/creating :$!";

The function sysopen() takes four parameters. The first parameter is the file handle parameter similar to the open() function. The second parameter is the file name without mode information. The third parameter is The mode parameter is composed of constants combined with the logical OR operation provided by the Fcntl module. The fourth parameter (optional) is an octal attribute value (0666 represents the data file, 0777 represents the program). sysopen() returns true if the file can be opened, false if the file fails to be opened.

Different from the open() function, sysopen() does not provide an abbreviation for mode description, but combines some constants. Moreover, each mode constant has a unique meaning, and they can only be combined through logical OR operations. You can set up a combination of multiple behaviors.

O_RDONLYRead-only

O_WRONLYWrite-only

O_RDWRReadingandwriting

O_APPENDWritesgototheendofthefile

O_TRUNCTruncatethefileifitexisted

O_CREATCreatethefileifitdidn'texist

O_EXCLErrorifthefilealreadyexisted(usedwithO_CREAT)

Use the sysopen() function when you need to be careful, for example, if You plan to add content to the file. If the file does not exist, without creating a new file, you can write like this:

sysopen(LOG,"/var/log/myPRog.log",O_APPEND,0666)

ordie"Can' topen/var/log/myprog.logforappending:$!";

The above is the classic usage of Perl: using Sysopen() for more control. For more related articles, please pay attention to the PHP Chinese website (www.php.cn )!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template