|
NAMEMIME::Body - the body of a MIME messageSYNOPSISBefore reading further, you should see MIME::Tools to make sure that you understand where this module fits into the grand scheme of things. Go on, do it now. I'll wait.Ready? Ok... Obtaining bodies### Get the bodyhandle of a MIME::Entity object: $body = $entity->bodyhandle; ### Create a body which stores data in a disk file: $body = new MIME::Body::File "/path/to/file"; ### Create a body which stores data in an in-core array: $body = new MIME::Body::InCore \@strings; Opening, closing, and using IO handles### Write data to the body: $IO = $body->open("w") || die "open body: $!"; $IO->print($message); $IO->close || die "close I/O handle: $!"; ### Read data from the body (in this case, line by line): $IO = $body->open("r") || die "open body: $!"; while (defined($_ = $IO->getline)) { ### do stuff } $IO->close || die "close I/O handle: $!"; Other I/O### Dump the ENCODED body data to a filehandle: $body->print(\*STDOUT); ### Slurp all the UNENCODED data in, and put it in a scalar: $string = $body->as_string; ### Slurp all the UNENCODED data in, and put it in an array of lines: @lines = $body->as_lines; Working directly with paths to underlying files### Where's the data? if (defined($body->path)) { ### data is on disk: print "data is stored externally, in ", $body->path; } else { ### data is in core: print "data is already in core, and is...\n", $body->as_string; } ### Get rid of anything on disk: $body->purge; DESCRIPTIONMIME messages can be very long (e.g., tar files, MPEGs, etc.) or very short (short textual notes, as in ordinary mail). Long messages are best stored in files, while short ones are perhaps best stored in core.This class is an attempt to define a common interface for objects which contain message data, regardless of how the data is physically stored. The lifespan of a "body" object usually looks like this:
You can write your own subclasses, as long as they follow the interface described below. Implementers of subclasses should assume that steps 2 and 3 may be repeated any number of times, and in different orders (e.g., 1-2-2-3-2-3-3-3-3-3-2-4). In any case, once a MIME::Body has been created, you ask to open it for reading or writing, which gets you an "i/o handle": you then use the same mechanisms for reading from or writing to that handle, no matter what class it is. Beware: unless you know for certain what kind of body you have, you should not assume that the body has an underlying filehandle. PUBLIC INTERFACE
SUBCLASSESThe following built-in classes are provided:Body Stores body When open()ed, class: data in: returns: -------------------------------------------------------- MIME::Body::File disk file IO::Handle MIME::Body::Scalar scalar IO::Handle MIME::Body::InCore scalar array IO::Handle MIME::Body::FileA body class that stores the data in a disk file. Invoke the constructor as:$body = new MIME::Body::File "/path/to/file"; In this case, the "path()" method would return the given path, so you could say: if (defined($body->path)) { open BODY, $body->path or die "open: $!"; while (<BODY>) { ### do stuff } close BODY; } But you're best off not doing this. MIME::Body::ScalarA body class that stores the data in-core, in a simple scalar. Invoke the constructor as:$body = new MIME::Body::Scalar \$string; A single scalar argument sets the body to that value, exactly as though you'd opened for the body for writing, written the value, and closed the body again: $body = new MIME::Body::Scalar "Line 1\nLine 2\nLine 3"; A single array reference sets the body to the result of joining all the elements of that array together: $body = new MIME::Body::Scalar ["Line 1\n", "Line 2\n", "Line 3"]; MIME::Body::InCoreA body class that stores the data in-core. Invoke the constructor as:$body = new MIME::Body::InCore \$string; $body = new MIME::Body::InCore $string; $body = new MIME::Body::InCore \@stringarray A simple scalar argument sets the body to that value, exactly as though you'd opened for the body for writing, written the value, and closed the body again: $body = new MIME::Body::InCore "Line 1\nLine 2\nLine 3"; A single array reference sets the body to the concatenation of all scalars that it holds: $body = new MIME::Body::InCore ["Line 1\n", "Line 2\n", "Line 3"]; Defining your own subclassesSo you're not happy with files and scalar-arrays? No problem: just define your own MIME::Body subclass, and make a subclass of MIME::Parser or MIME::ParserBase which returns an instance of your body class whenever appropriate in the "new_body_for(head)" method.Your "body" class must inherit from MIME::Body (or some subclass of it), and it must either provide (or inherit the default for) the following methods... The default inherited method should suffice for all these: new binmode [ONOFF] path The default inherited method may suffice for these, but perhaps there's a better implementation for your subclass. init ARGS... as_lines as_string dup print purge The default inherited method will probably not suffice for these: open NOTESOne reason I didn't just use IO::Handle objects for message bodies was that I wanted a "body" object to be a form of completely encapsulated program-persistent storage; that is, I wanted users to be able to write code like this...### Get body handle from this MIME message, and read its data: $body = $entity->bodyhandle; $IO = $body->open("r"); while (defined($_ = $IO->getline)) { print STDOUT $_; } $IO->close; ...without requiring that they know anything more about how the $body object is actually storing its data (disk file, scalar variable, array variable, or whatever). Storing the body of each MIME message in a persistently-open IO::Handle was a possibility, but it seemed like a bad idea, considering that a single multipart MIME message could easily suck up all the available file descriptors on some systems. This risk increases if the user application is processing more than one MIME entity at a time. SEE ALSOMIME::ToolsAUTHOREryq (eryq@zeegee.com), ZeeGee Software Inc (http://www.zeegee.com). David F. Skoll (dfs@roaringpenguin.com) http://www.roaringpenguin.comAll rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Thanks to Achim Bohnet for suggesting that MIME::Parser not be restricted to the use of FileHandles. #------------------------------ 1;
Visit the GSP FreeBSD Man Page Interface. |