Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

It seems both do the same thing, huh?

Can someone show me an example where they do different job?

like image 724
lexer Avatar asked Sep 20 '11 14:09

lexer


2 Answers

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.

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.

like image 150
Blagovest Buyukliev Avatar answered Nov 01 '22 19:11

Blagovest Buyukliev


To quote perlopentut:

If you want the convenience of the shell, then Perl's open is definitely the way to go. 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. 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. The difference is that open() in Perl has some niceties built in that make opening, piping and redirection very easy. At the same time, though, open() takes away some flexibility. It has none of the Fcntl functionality available in sysopen(), nor does it have the masking functionality.

Most situations just need open().

like image 24
Jonathan M Avatar answered Nov 01 '22 19:11

Jonathan M