Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: sysopen vs. open

by tomfahle (Priest)
on May 15, 2009 at 06:07 UTC ( #764188=note: print w/replies, xml ) Need Help??


in reply to sysopen vs. open

Hi,
something like

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

Replies are listed 'Best First'.
Re^2: sysopen vs. open
by tilly (Archbishop) on May 15, 2009 at 15:25 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-10-26 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?