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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.