"Randy W. Sims" <[EMAIL PROTECTED]> writes:
Harry Putnam wrote:
I'm writing a home boy mail/news search tool and wondered if there is a cononical way to handle folded or indented header lines.
There are modules to help read mail headers, but if you want something simple you can modify the following example:
#!/usr/bin/perl
use strict; use warnings;
open(MAIL, 'mail.msg') or die $!;
my $line; LINE: while (<MAIL>) { chomp; if (1../^$/) { unless (/^\s/) { print "$line\n"; $line = $_; } else { s/^\s+/ /; $line .= $_; next LINE; } } }
You can perform your matches at the point where I placed the print statement; at that point a complete line is assembled.
Nice, that fixes the folds alright. But if I might pester you a bit further, I don't recognize the syntax in at least two parts.
I see what the result is but can you explain in english what these lines do:
if (1../^$/) and $line .= $_;
I'm guessing the first is something similar to: if(!/^$/)
This is one of the more unusual uses of the range operator. In scalar context it operates similar to the line addressing of sed and awk. In this particular instance it returns true if we are currently between the first line of the file and the first blank line of the file, I.E. we are in the header. See 'perldoc perlop', the section on the range operator for more info.
But have no guess on the second one.
This is one of perl's compound assignment operators. It is equivelant to
$line = $line . $_;
i.e. it concatenates the two lines. You can read more about it in 'perldoc perlop', the section on the assignment operator.
If you need more examples/explanations beyond what is in the docs, just ask and we can try to provide a more detailed answer.
Regards, Randy.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>