usefcntl;
sysopen(fh,$filename,o_rdwr|o_creat,0666)
ordiecan'topen$filenameforreading/writing/creating:$!;
函数sysopen()带有四个参数,第一个是同open()函数类似的文件句柄参数,第二个参数是不带模式信息的文件名,第三个参数是模式参数,由fcntl模块提供的逻辑or运算组合起来的常数构成,第四个参数(可选),为八进制属性值(0666表示数据文件,0777表示程序)。如果文件可以被打开,sysopen()返回true,如果打开失败,则返回false。
不同于open()函数,sysopen()不提供模式说明的简写方式,而是把一些常数组合起来,而且,每个模式常数有唯一的含义,只有通过逻辑or运算才能将它们组合起来,你可以设置多个行为的组合。
o_rdonlyread-only
o_wronlywrite-only
o_rdwrreadingandwriting
o_appendwritesgototheendofthefile
o_trunctruncatethefileifitexisted
o_creatcreatethefileifitdidn'texist
o_exclerrorifthefilealreadyexisted(usedwitho_creat)
当你需要小心行事的时候,就使用sysopen()函数,例如,如果你打算添加内容到文件中,如果文件不存在,不创建新文件,你可以这样写:
sysopen(log,/var/log/myprog.log,o_append,0666)
ordiecan'topen/var/log/myprog.logforappending:$!;
以上就是perl的经典用法:用sysopen()进行更多的控制的内容。
