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

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

Dec 16, 2016 pm 01:52 PM

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 )!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)