in reply to TIMTOWTDI Challenge: Open a file

Object-oriented (adapted from IO::File synopsis):

use IO::File; $fh = IO::File->new("< file"); $fh = IO::File->new("file", "r"); $fh = IO::File->new("file", O_WRONLY|O_APPEND);

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: TIMTOWTDI Challenge: Open a file
by stonecolddevin (Parson) on Apr 27, 2006 at 23:30 UTC

    oooo....i like that...that's pretty...and clean.

    my question is...what kind of modules are there for operating systems that don't support file locking?

    I know there's quite a few methods for checking and adapting for such, but has anyone put together a module encompassing these routines?

    meh.

      According to perlport, flock is available on WinNT (and later). You should be able to check Config for d_flock.

      > perl -MConfig -le "print $Config{d_flock}" define

      No guarantees on how good that is for protecting against anything other than other Perl programs using flock. You might have to use Win32API::File for more direct control.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Windows supports both advisory and mandatory file locking, and can perform a non-blocking lock check.

      It even uses mandatory exclusive locks by default. You can relax the lock and allow others to do any combination of the following: delete the file (FILE_SHARE_DELETE), read from the file (FILE_SHARE_READ) and write to the file (FILE_SHARE_WRITE).

      Finally, Perl's flock works perfectly fine in Windows, including LOCK_NB.