in reply to Text output is on one line

if($opsys eq "unix"){flock(FILE, 2);} else{binmode(FILE);}

This is a curious construct; the logic appears to be "lock the filehandle on systems described by the identifier 'unix', set binmode on the filehandle on other systems".

If you think you might need binmode, set it anyway; it's a no-op on systems that don't make the distinction. And try not to mix operations like that within an if {...} else {...}. Future maintenance programmers will not thank you for it.

Final point; don't use 'magic' numbers, as you do in flock() here, particularly if there's some likelyhood of cross-platform use. Use the constants provided by the Fcntl module instead.

use Fcnlt qw(:flock); open(FILE, "+<$bookfile") || &debug("Oops, I cannot read the bookfile $bookfile: $!"); if($opsys eq "unix"){flock(FILE, LOCK_EX) # Error-check here.} else { # What do we do on other systems? } binmode FILE;

Replies are listed 'Best First'.
Re^2: Text output is on one line
by Aristotle (Chancellor) on Dec 11, 2002 at 14:21 UTC
    Better yet:
    use Fcntl qw(:flock); binmode FILE; eval { flock FILE, LOCK_EX };
    eval will catch the exception caused by using flock where not supported and keep the program from dying. Just checking for $opsys eq "unix" is not sufficient - Perl does have flock on Windows NT f.ex, and there might be pathological cases where a system that identifies itself as unix does not. Catching the exception is a robust way to have it work wherever possible.

    Makeshifts last the longest.