Background: I'm working on reformating .mbox files to convert email archives to HTML and to PDA compatible text. I'm running into problems with the MIME types "multipart/mixed" and "multipart/related". These are umbrella types that can hold an assortment of simple types, like "text/plain", and "image/jpg". However they can also hold other multipart types, which can happen when someone backquotes the entirety of a previous multipart message.
There is also the "multipart/alternative" type, but these are always collections of simple types where the user agent chooses one and ignores the rest-- they are never re-entrant.
My code is something like this (following is simplified to keep it short and on point):
sub handleBody { my ($type, $body) = @_; my $superbody = ''; if ( $type =~ m{multipart}i ) { my ( @parts ) = splitOnBoundary($type, $body); if ( $type =~ m{alternative}i ) { # code to find the $best of the alternatives in @parts ($type, $body) = handlePart($parts[$best]) processSimpleType($type, $body); return $body; } else { # PLACE WHERE MY HEAD HURTS foreach (@parts) { $superbody .= handleBody(handlePart($_) ); } return $superbody; } } else { # handle a simple type processSimpleType($type, $body) return $body } }
sub handlePart { my $part = shift; my ($head, $body) = split /^$/m, $part, 2; # treat exceptions as a type of its own: my $type = '[NONE STATED]'; if ( $head =~ m{^Content-Type: (.*)$}mi ) { $type = $1; } return ($type, $body); }
Hmm, as I wrote this, I discovered the apparent need for $superbody, and I think I may have solved my logic problem. So the first of my two questions:
1) Does the above code look right?
A major difficulty is that I'm dealing with archives where some of the messages aren't fully compliant with the MIME standard and I can't tell whether the bugs I've got are in the logic or because I need to tweak the regexes to handle the special cases. Which brings me to the other, more important, question:
2) Is there a better tool for designing re-entrant code other than pseudocode? How do people who do a lot of this kind of thing work out the design?
-- Will
_______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs