9

It seems both do the same thing, huh?

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

2
  • 2
    It's not an internal difference, it's an external difference, like Blagovest Buyukliev said.
    – ikegami
    Sep 20, 2011 at 17:44
  • @lexer, I've added a bit more explanation. Hope it helps.
    – Jonathan M
    Sep 20, 2011 at 19:16

2 Answers 2

18

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.

3
  • I still don't see the difference though
    – lexer
    Sep 20, 2011 at 14:07
  • 2
    @lexer: there's really nothing more to say - the rest is to understand what a system call is, and what the particular interface of open(2) is, all of which is OS stuff, not Perl-specific. Sep 20, 2011 at 15:02
  • 2
    @lexer: The answer is spot on. In case you need a real-life exapmle, consider O_WRONLY|O_EXCL|O_CREAT combo, i.e. "create and write if not exists". Using -f ... or open ... instead is just asking for race condition.
    – Dallaylaen
    Sep 20, 2011 at 15:28
12

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().

2
  • Can you provide an example of Fcntl and masking functionality available in sysopen?
    – lexer
    Sep 21, 2011 at 12:15
  • There are some great examples of masks here: perldoc.perl.org/perlopentut.html#Permissions-agrave-la-mode , and the section in the same link just above where the link lands has great examples of the Fcntl features. That section begins with the phrase "And here are things you can do with sysopen that you cannot do with a regular open." Enjoy!
    – Jonathan M
    Sep 21, 2011 at 14:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.