[ale] Easy way to cut body of email in perl

James P. Kinney III jkinney at localnetsolutions.com
Thu Jan 5 13:24:43 EST 2006


On Thu, 2006-01-05 at 13:00 -0500, fletch at phydeaux.org wrote:
> > I looked at Mail::Internet and unless I read the perldoc wrong I did not
> > see a way I could pass the whole email to a new object and then have it
> > parsed.
> [... snip ...]
> > new ( [ ARG ], [ OPTIONS ] )
> >
> >         ARG is optional and may be either a file descriptor (reference
> >         to a GLOB) or a reference to an array. If given the new object
> >         will be initialized with headers and body either from the array
> >         of read from the file descriptor.
> 
> You're reading the message contents from STDIN so just pass that to the
> new method.
> 
> my $msg = Mail::Internet->new( \*STDIN );
> 
> 
> If you'd already read the whole thing into an array:
> 
> my @lines = <>;
> 
> You could pass a reference to that:
> 
> my $msg = Mail::Internet->new( \@lines );
> 
> 
> Or if you'd read it into a scalar you could split on \n and pass that:
> 
> my $msg = Mail::Internet->new( [ split( "\n", $contents ) ] );

If you read the array into a scalar $contents then just split on the
first, standalone blank line.

my ($headers, $msg) = split(/^\n$/, $contents);

or you can take an array slice from the original @lines by finding the
first element with a just the \n

$looking = '1';
$n='0';
while ($looking){if ($lines[$n] =~ m/^\n$/){$looking='0'}}
$msg = @lines($n+1,$#lines);

or.....

perl is phun


> 
-- 
James P. Kinney III          \Changing the mobile computing world/
CEO & Director of Engineering \          one Linux user         /
Local Net Solutions,LLC        \           at a time.          /
770-493-8244                    \.___________________________./
http://www.localnetsolutions.com

GPG ID: 829C6CA7 James P. Kinney III (M.S. Physics)
<jkinney at localnetsolutions.com>
Fingerprint = 3C9E 6366 54FC A3FE BA4D 0659 6190 ADC3 829C 6CA7
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part




More information about the Ale mailing list