Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

sysopen vs. open

by bichonfrise74 (Vicar)
on May 14, 2009 at 23:56 UTC ( [id://764170]=perlquestion: print w/replies, xml ) Need Help??

bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I came across a function called sysopen in one of our scripts and I was wondering if there is an advantage of sysopen vs. open.

If so, which one is better or is there a situation where one is better than the other?

I've always used open and I'm not sure if I should start using sysopen.

Replies are listed 'Best First'.
Re: sysopen vs. open
by jwkrahn (Abbot) on May 15, 2009 at 00:10 UTC

    Perl's open is equivalent to C's fopen and Perl's sysopen is equivalent to C's open.

      I'm not very familiar with C. So, I have to ask... in which situation is open better than sysopen or vice-versa?
        At a command prompt type perldoc -f sysopen and perldoc -f open. Or compare them on http://perldoc.perl.org/index-functions.html.

        My summary is that sysopen is a lower-level interface that can't ever be removed because of backwards compatibility, while open is the one that I would suggest people use.

      Not so. Both create buffered handles like fopen(3), not mere file descriptors like open(2)

      The difference is the args you can pass to them.

      That's not complete. Perl's open is equivalent to the union of C's fopen and popen.
Re: sysopen vs. open
by tomfahle (Priest) on May 15, 2009 at 06:07 UTC
      Why do you want to have a file that you know you just created? Most of the time when I want that I really want File::Temp. For locking you have other options available, including flock. You can complain that flock often doesn't work well over networked file systems. But sysopen also doesn't work well on those systems.

      So yes, there is a task that you can do with sysopen that you can't with open. But I have yet to encounter a situation where I thought that sysopen was the best option.

Re: sysopen vs. open
by nikosv (Deacon) on May 15, 2009 at 04:54 UTC
    the advantage of sysopen is the flags that it takes such as O_WRONLY|O_CREAT which allow for finer control. A common misconception is that sysopen creates an unbuffered handle; it does not
Re: sysopen vs. open
by JavaFan (Canon) on May 15, 2009 at 07:25 UTC
    Did you read the manual pages about open, sysopen, and perlopentut?

      ...from reading man pages you only learn, but you don't get XP :)

        I'm not here to get XP, but for the learning. There's no point in earning XP if I'm not learning, what will I do with it? :)
      I did read the perlopentut and man pages for open and sysopen. And as jwkrahn mentioned, sysopen is equivalent to C's open, but I'm still not sure what situation is open better than sysopen.
        That depends on your definition of better. Some people consider less typing for common situation to be better. Further more, there are things that can be done with open that cannot be done with sysopen. And there things that can be done with sysopen that cannot be done with open.
      Interestingly, none of those talk about the return values for sysopen. I'm going to guess that a falsey value is a fail.
        obviously, rou read different documentation than I. All three (open, sysopen and perlopentut) explicitly say that undef is returned on failure, which is, as you 'guessed', a falsey value.

        Edit: added the words in italics

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764170]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found