Ok, well I'm sure that this is really basic, but I am having a nightmare working it out.
I've looked at Mail::Folder::Mbox, but I don't like this limitation (it's the same problem that I'm having) :
<quote>* Mark it as readonly if the folder is not writable.</QUOTE>

I just want to truncate a mailbox in place (standard UNIX format) after copying all the mail out of it for a play by mail game that I'm writing.

Here's the sub that I'm having trouble with (see the bit commented out with 4 sh's - I've left my other comments in to show how I'm thinking):

sub get_mail { unless (-s $mailspool) { # Nothing in the mailbox? Don't bot +her then $debug and print LOG "No mail in mailspool\n"; return; } open(MAILBOX, "<$mailspool") or &loganddie("$mailspool exists but I + can't open it : $!"); flock(MAILBOX, LOCK_EX|LOCK_NB) or do {$failed_locks++; return 0}; # Not getting the lock is not all that fatal - we can try later # If we get here, then we can finally do something with the mailspo +ol. # First though, we have to open the file we're moving it to. opendir (PROCESSDIR, $processdir) or &loganddie("Someone broke $pro +cessdir : $!"); my @allfiles = grep !/^\.\.?\z/, readdir PROCESSDIR; closedir PROCESSDIR; my $output_file = &POSIX::tmpnam(); $output_file =~ s/\/tmp\/(.*)/$1/; # remove /tmp/ from string open(PENDING, ">>$processdir/$output_file") or &loganddie("Can't wr +ite to $processdir/$output_file : $!"); $debug and print "opened file\n"; flock(PENDING, LOCK_EX|LOCK_NB) or return 0; $debug and print "got lock\n"; seek(PENDING,0,2) or return 0; # in case some other process append +ed $debug and print "did seek\n"; print PENDING while (<MAILBOX>); $debug and print "read mailbox\n"; close PENDING or &loganddie("man there is some crazy sh|t going on\ +n\tcouldn't close $processdir/$output_file : $!"); $debug and print "closed pending\n"; #### OK, somehow we now have to truncate the file without unlinking + it (don't have write on /var/spool/mail) or releasing the lock (what + if more mail comes in between us releasing the lock and truncating t +he file?) close MAILBOX; $debug and print "closed MAILBOX\n"; return; }

Can anyone help? Cheer

setantae@eidosnet.co.uk|setantae|www.setantae.uklinux.net


In reply to Truncate a mailbox in place by setantae

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.