open() not closing open filehandle before reopening 
Author Message
 open() not closing open filehandle before reopening

I've written a Perl script that sorts incoming mail by content, essentially
duplicating the effects of the MH command "slocal".  The heart of the
script is the following bit of code:

foreach $mesg ($firstmsg..$firstmsg+$l-1) {
        open(MESG,"$mailpath/$inbox/$mesg") || next;
        while (<MESG>) {
                last if /^$/ && !$numbodies;
                $_ .= <MESG> while $_ =~ /,$/;
                study;

                        if ($line->[0] =~ /^body$/i) {
                                $match = /$line->[1]/i;
                        } else {
                                next unless 1../^$/;
                                $match = /^$line->[0]\s*:.*$line->[1]/i;
                        }
                        $match and $matched{$line}->{$mesg} = 1;
                }
        }

Quote:
}

close(MESG);

.maildelivery file; each element is a reference to an anonymous array
containing the five entries from the corresponding line, eg. a delivery
file like this:

to      developer       mbox    A       developer
from    so-and-so       destroy A       -

is scanned into

           ["from","so-and-so","destroy","A","-"] );

If no delivery commands specify a body search, the message scan is stopped
at the first blank line (the "last if /^$/ ..." line above).

Anyway, the code wasn't working correctly, and further investigation showed
that when a message scan was aborted at a blank line, the open() function
wasn't closing the MESG filehandle before reopening it, and subsequent
reads from <MESG> were getting data from the old message.  Every blank line
in the body of the original message caused another false open(); a new
message wasn't actually opened until the original message reached
end-of-file.  This caused a number of messages equal to the number of blank
lines in the original message to simply be skipped.

The reason I went into such detail about the script is that this sample
script that I wrote, using (apparently) the same logic as the first,
functions as expected:

foreach $file ("numbers","letters") {
        open(FILE,$file) || die "$!\n";
        while (<FILE>) {
                print("Skipping to next file\n"), last if /^$/;
                print;
        }

Quote:
}

...where "numbers" and "letters" are files with groups of numbers and
letters (respectively, of course) separated by occasional blank lines.

I was able to get the original script to work by swapping the final two
lines (doing an explicit close() before each open()), but I'd like to know
why the original didn't work.

Can someone tell me why the original script doesn't work?  I'm using Perl
5.001m, if it makes a difference.  Many thanks in advance.

--
Sean McAfee         |    "Mind if I smoke?"



Tue, 03 Mar 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. overhead on filehandle opening/closing

2. Filehandle: open(FILE) while{} close(FILE)

3. How can I close ALL open filehandles?

4. Any reason not to use close() with open()?

5. 4.035 BUG: STDIN not closed on reopen

6. opening a file, NOT a filehandle

7. Perlbug 20010213.008 status update open->closed but nothing else

8. Questions about $%, $=, $-, and write vs open and close

9. open() close() same file many times

10. regex match for same number of opening and closing brackets

11. Opening/closing sockets

12. Discerning an Open and Closed Socket...

 

 
Powered by phpBB® Forum Software