There are a couple of different approaches to this. (Of course there are; it's Perl :-) )
First option: use sysopen() to create the file with the specified permissions, then use store_fd() and fd_retrieve(). See also the Perl open() tutorial, particularly the "Permissions a la mode" section.
Second option: use umask() to limit the file permissions. Don't forget to set it back to its original value if you're working with multiple files.
Third option: use chmod() to set the file permissions manually on a pre-existing file.
The first option is conceptually better because it allows the user to tighten permissions further by controlling the umask themselves. (Try help umask at a shell prompt. The umask set there applies to all programs run from the shell. Again, see perlopentut.)
发布评论
评论(1)
对此有几种不同的方法。 (当然有;它是 Perl :-) )
第一个选项:使用
sysopen()
创建具有指定权限的文件,然后使用store_fd()
和fd_retrieve()
。另请参阅 Perl open() 教程,特别是“Permissions时尚”部分。第二种选择:使用
umask()
来限制文件权限。如果您正在处理多个文件,请不要忘记将其设置回原始值。第三种选择:使用
chmod()
来对预先存在的文件手动设置文件权限。第一个选项在概念上更好,因为它允许用户通过自己控制 umask 来进一步收紧权限。 (在 shell 提示符处尝试
help umask
。那里设置的 umask 适用于从 shell 运行的所有程序。再次参见 perlopentut。)There are a couple of different approaches to this. (Of course there are; it's Perl :-) )
First option: use
sysopen()
to create the file with the specified permissions, then usestore_fd()
andfd_retrieve()
. See also the Perl open() tutorial, particularly the "Permissions a la mode" section.Second option: use
umask()
to limit the file permissions. Don't forget to set it back to its original value if you're working with multiple files.Third option: use
chmod()
to set the file permissions manually on a pre-existing file.The first option is conceptually better because it allows the user to tighten permissions further by controlling the umask themselves. (Try
help umask
at a shell prompt. The umask set there applies to all programs run from the shell. Again, see perlopentut.)