I'm learning (or trying to learn) how to work with filehandles passed along through returns and params.
I believe I have the fileopen and return working. At least is seems to be working.
The part I'm messing up on is the file locking part.
From flock manpage:
sub lock {
flock(MBOX,LOCK_EX);
# and, in case someone appended
# while we were waiting...
seek(MBOX, 0, 2);
}
sub unlock {
flock(MBOX,LOCK_UN);
}
open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
or die "Can't open mailbox: $!";
lock();
print MBOX $msg,"\n\n";
unlock();
Below was my attempt at making this work. If a kind hearted monk could help unscramble this I'd appreciate it.
sub fileopen {
my($filename) = @_;
local *MW;
my($openokay) = open(MW,"+<$filename");
if (!$openokay) {
return 0;
}
lock(*MW);
seek(MW,0,0);
return *MW;
}
sub lock {
my($nick) = @_;
flock({$nick},LOCK_EX);
# and, in case someone appended
# while we were waiting...
seek({$nick}, 0, 2);
}
sub unlock {
my($nick) = @_;
flock({$nick},LOCK_UN);
}
Thanks in advance.
Claude