Re: sysopen vs. open
by jwkrahn (Abbot) on May 15, 2009 at 00:10 UTC
|
| [reply] [d/l] [select] |
|
I'm not very familiar with C. So, I have to ask... in which situation is open better than sysopen or vice-versa?
| [reply] |
|
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
That's not complete. Perl's open is equivalent to the union of C's fopen and popen.
| [reply] |
Re: sysopen vs. open
by tomfahle (Priest) on May 15, 2009 at 06:07 UTC
|
unless (-e $file) {
open(FH,">", "$file") or die $!;
}
is almost always wrong as it's open to race conditions.
See Perl Training Australia File test operators, the garden path of race conditions and PerlMonks Avoiding race condition with sysopen for more details.
So you're better off avoiding file tests:
use Fcntl;
# Open $file for writing unless it already exists
sysopen(FH, $file, O_WRONLY|O_EXCL|O_CREAT) or die $!;
HTH,
Thomas
| [reply] [d/l] [select] |
|
| [reply] |
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
| [reply] |
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? | [reply] |
|
| [reply] |
|
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? :)
| [reply] |
|
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.
| [reply] |
|
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.
| [reply] |
|
Interestingly, none of those talk about the return values for sysopen.
I'm going to guess that a falsey value is a fail.
| [reply] |
|
| [reply] [d/l] |