MIME::Body(3)
π MIME::Body(3) β User Contributed Perl Documentation π
okay so bestie, welcome to MIME::Body(3) β the body of a MIME message. This is from User Contributed Perl Documentation, and it's about to be your new obsession. no cap. π
β¨ NAME β¨
MIME::Body β the body of a MIME message. that's it, that's the name. iconic. π
πΈ SYNOPSIS πΈ
Before 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. (no really, I'll wait. I have time. π
)
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;
π DESCRIPTION π
MIME 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. it's giving "know your storage limits" energy. πΎ
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:
- Body object is created by a
MIME::Parserduring parsing. It's at this point that the actualMIME::Bodysubclass is chosen, andnew()is invoked. (For example: if the body data is going to a file, then it is at this point that the classMIME::Body::File, and the filename, is chosen). - Data is written to the body (usually by the MIME parser) like this: The body is opened for writing, via
open("w"). This will trash any previous contents, and return an "I/O handle" opened for writing. Data is written to this I/O handle, viaprint(). Then the I/O handle is closed, viaclose(). - Data is read from the body (usually by the user application) like this: The body is opened for reading by a user application, via
open("r"). This will return an "I/O handle" opened for reading. Data is read from the I/O handle, viaread(),getline(), orgetlines(). Then the I/O handle is closed, viaclose(). - Body object is destructed.
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). literally chaos, and we love that for them. π₯
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. periodt. π
π PUBLIC INTERFACE π
new ARGS...β Class method, constructor. Create a new body. AnyARGSare sent toinit(). slay.init ARGS...β Instance method, abstract, initiallizer. This is called automatically bynew(), with the arguments given tonew(). The arguments are optional, and entirely up to the subclass. The default method does nothing, (yes, that trailing comma is real, leave it be).as_linesβ Instance method. Return the contents of the body as an array of lines (each terminated by a newline, with the possible exception of the final one). Returns empty on failure (NB: indistinguishable from an empty body!). Note: the default method gets the data via repeatedgetline()calls; your subclass might wish to override this.as_stringβ Instance method. Return the body data as a string (slurping it into core if necessary). Best not to do this unless you're sure that the body is reasonably small! Returns empty string for an empty body, andundefon failure. Note: the default method usesprint(), which gets the data via repeatedread()calls; your subclass might wish to override this.binmode [ONOFF]β Instance method. With argument, flags whether or notopen()should return an I/O handle which hasbinmode()activated. With no argument, just returns the current value.is_encoded [ONOFF]β Instance method. If set to yes, no decoding is applied on output. This flag is set byMIME::Parser, if the parser runs indecode_bodies(0)mode, so the content is handled unmodified.dupβ Instance method. Duplicate the bodyhandle. Beware: external data in bodyhandles is not copied to new files! Changing the data in one body's data file, or purging that body, will affect its duplicate. Bodies with in-core data probably need not worry.open READWRITEβ Instance method, abstract. This should do whatever is necessary to open the body for either writing (ifREADWRITEis"w") or reading (if mode is"r"). This method is expected to return an "I/O handle" object on success, andundefon error. An I/O handle can be any object that supports a small set of standard methods for reading/writing data. See theIO::Handleclass for an example.path [PATH]β Instance method. If you're storing the body data externally (e.g., in a disk file), you'll want to give applications the ability to get at that data, for cleanup. This method should return the path to the data, orundefif there is none. Where appropriate, the path should be a simple string, like a filename. With argument, sets thePATH, which should beundefif there is none.print FILEHANDLEβ Instance method. Output the body data to the given filehandle, or to the currently-selected one if none is given.purgeβ Instance method, abstract. Remove any data which resides external to the program (e.g., in disk files). Immediately after apurge(), thepath()should returnundefto indicate that the external data is no longer available.
π SUBCLASSES π
The 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::File
A 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. (like, why would you? but okay.)
π§ MIME::Body::Scalar
A 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::InCore
A 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 subclasses
So 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
β¨ NOTES β¨ (aka the tea β)
okay so bestie, real talk: why did the author not just use IO::Handle objects for message bodies? π because the whole vision was for a "body" object to be a form of completely encapsulated program-persistent storage. like, fully self-contained. a little mystery box π. the user should be able to write code like this and not think about it even once:
### 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;
β¦and that's it. that's the whole vibe. π they shouldn't have to know anything more about how the $body object is actually storing its data β could be a disk file, a scalar variable, an array variable, or whatever. doesn't matter. not your problem. the object handles it. slay π
now, the alternative: storing the body of each MIME message in a persistently-open IO::Handle. technically possible? sure. but it seemed like a bad idea, and here's why, no cap: a single multipart MIME message could easily suck up all the available file descriptors on some systems. π all of them. gone. and this risk increases if the user application is processing more than one MIME entity at a time. so yeah β encapsulated storage, iconic decision, periodt. π
π SEE ALSO π
MIME::Tools
π AUTHOR π
Eryq (eryq@zeegee.com), ZeeGee Software Inc (http://www.zeegee.com). Dianne Skoll (dianne@skoll.ca)
All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π«Ά shoutout to Achim Bohnet for suggesting that MIME::Parser not be restricted to the use of FileHandles. king behavior, honestly.
#------------------------------ 1;
and that's a wrap on this page, bestie!! π brought to you by perl v5.42.0, dated 2026-02-11 β go forth and handle your MIME bodies with main character energy π¦π
π¦ where this page comes from
- fedora 44: perl-MIME-tools 5.517-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms