简体   繁体   English

Perl中open和sysopen有什么区别?

[英]What's the difference between open and sysopen in Perl?

It seems both do the same thing, huh? 看起来两者都做同样的事情,对吧?

Can someone show me an example where they do different job? 有人能告诉我一个他们做不同工作的例子吗?

sysopen is a thin wrapper around the open(2) kernel system call (the arguments correspond directly), whereas open is a higher-level wrapper which enables you to do redirections, piping, etc. sysopenopen(2)内核系统调用的一个瘦包装器(参数直接对应),而open是一个更高级别的包装器,它允许你进行重定向,管道等。

Unless you are working with a specific device that requires some special flags to be passed at open(2) time, for ordinary files on disk you should be fine with open . 除非您正在使用需要在open(2)时传递一些特殊标志的特定设备,否则对于磁盘上的普通文件,您应该可以open

To quote perlopentut : 引用perlopentut

If you want the convenience of the shell, then Perl's open is definitely the way to go. 如果你想要shell的便利性,那么Perl的开放绝对是你要走的路。 On the other hand, if you want finer precision than C's simplistic fopen(3S) provides you should look to Perl's sysopen, which is a direct hook into the open(2) system call. 另一方面,如果你想要比C的简单fopen(3S)提供更精确的精度,你应该看看Perl的sysopen,它是open(2)系统调用的直接挂钩。 That does mean it's a bit more involved, but that's the price of precision. 这确实意味着它涉及更多,但这是精确的代价。

Since Perl is written in C, both methods likely end up making the open(2) system call. 由于Perl是用C语言编写的,因此两种方法都可能最终进行open(2)系统调用。 The difference is that open() in Perl has some niceties built in that make opening , piping and redirection very easy. 不同之处在于Perl中的open()内置了一些细节,使得打开管道重定向变得非常容易。 At the same time, though, open() takes away some flexibility. 但与此同时, open()带走了一些灵活性。 It has none of the Fcntl functionality available in sysopen() , nor does it have the masking functionality. 它没有sysopen()可用的Fcntl功能,也没有屏蔽功能。

Most situations just need open() . 大多数情况只需要open()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM