{"id":7447,"date":"2022-12-20T19:38:08","date_gmt":"2022-12-20T22:38:08","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/mimebody-man3\/"},"modified":"2022-12-20T19:38:08","modified_gmt":"2022-12-20T22:38:08","slug":"mimebody-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/mimebody-man3\/","title":{"rendered":"MIME::Body (man3)"},"content":{"rendered":"<h1 align=\"center\">MIME::Body<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#PUBLIC INTERFACE\">PUBLIC INTERFACE<\/a><br \/> <a href=\"#SUBCLASSES\">SUBCLASSES<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">MIME::Body \u2212 the body of a MIME message<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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\u2019ll wait.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Ready? Ok&#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Obtaining bodies<\/b> <br \/> ### Get the bodyhandle of a MIME::Entity object: <br \/> $body = $entity\u2212>bodyhandle; <br \/> ### Create a body which stores data in a disk file: <br \/> $body = new MIME::Body::File &#8220;\/path\/to\/file&#8221;; <br \/> ### Create a body which stores data in an in\u2212core array: <br \/> $body = new MIME::Body::InCore @strings;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Opening, closing, and using <small>IO<\/small> handles<\/b> <br \/> ### Write data to the body: <br \/> $IO = $body\u2212>open(&#8220;w&#8221;) || die &#8220;open body: $!&#8221;; <br \/> $IO\u2212>print($message); <br \/> $IO\u2212>close || die &#8220;close I\/O handle: $!&#8221;; <br \/> ### Read data from the body (in this case, line by line): <br \/> $IO = $body\u2212>open(&#8220;r&#8221;) || die &#8220;open body: $!&#8221;; <br \/> while (defined($_ = $IO\u2212>getline)) { <br \/> ### do stuff <br \/> } <br \/> $IO\u2212>close || die &#8220;close I\/O handle: $!&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Other I\/O<\/b> <br \/> ### Dump the ENCODED body data to a filehandle: <br \/> $body\u2212>print(*STDOUT); <br \/> ### Slurp all the UNENCODED data in, and put it in a scalar: <br \/> $string = $body\u2212>as_string; <br \/> ### Slurp all the UNENCODED data in, and put it in an array of lines: <br \/> @lines = $body\u2212>as_lines;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Working directly with paths to underlying files<\/b> <br \/> ### Where&#8217;s the data? <br \/> if (defined($body\u2212>path)) { ### data is on disk: <br \/> print &#8220;data is stored externally, in &#8220;, $body\u2212>path; <br \/> } <br \/> else { ### data is in core: <br \/> print &#8220;data is already in core, and is&#8230;n&#8221;, $body\u2212>as_string; <br \/> } <br \/> ### Get rid of anything on disk: <br \/> $body\u2212>purge;<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><small>MIME<\/small> 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8220;body&#8221; object usually looks like this:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>1.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p><b>Body object is created by a MIME::Parser during parsing.<\/b> It\u2019s at this point that the actual MIME::Body subclass is chosen, and <b>new()<\/b> is invoked. (For example: if the body data is going to a file, then it is at this point that the class MIME::Body::File, and the filename, is chosen).<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>2.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p><b>Data is written to the body<\/b> (usually by the <small>MIME<\/small> parser) like this: The body is opened for writing, via &#8220;open(&#8220;w&#8221;)&#8221;. This will trash any previous contents, and return an &#8220;I\/O handle&#8221; opened for writing. Data is written to this I\/O handle, via <b>print()<\/b>. Then the I\/O handle is closed, via <b>close()<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>3.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p><b>Data is read from the body<\/b> (usually by the user application) like this: The body is opened for reading by a user application, via &#8220;open(&#8220;r&#8221;)&#8221;. This will return an &#8220;I\/O handle&#8221; opened for reading. Data is read from the I\/O handle, via <b>read()<\/b>, <b>getline()<\/b>, or <b>getlines()<\/b>. Then the I\/O handle is closed, via <b>close()<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>4.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p><b>Body object is destructed.<\/b><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">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\u22122\u22122\u22123\u22122\u22123\u22123\u22123\u22123\u22123\u22122\u22124).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In any case, once a MIME::Body has been created, you ask to open it for reading or writing, which gets you an &#8220;i\/o handle&#8221;: you then use the same mechanisms for reading from or writing to that handle, no matter what class it is.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Beware: unless you know for certain what kind of body you have, you should <i>not<\/i> assume that the body has an underlying filehandle.<\/p>\n<h2>PUBLIC INTERFACE <a name=\"PUBLIC INTERFACE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">new <small>ARGS&#8230;<\/small><\/p>\n<p style=\"margin-left:17%;\"><i>Class method, constructor.<\/i> Create a new body. Any <small>ARGS<\/small> are sent to <b>init()<\/b>.<\/p>\n<p style=\"margin-left:11%;\">init <small>ARGS&#8230;<\/small><\/p>\n<p style=\"margin-left:17%;\"><i>Instance method, abstract, initiallizer.<\/i> This is called automatically by &#8220;new()&#8221;, with the arguments given to &#8220;new()&#8221;. The arguments are optional, and entirely up to the subclass. The default method does nothing,<\/p>\n<p style=\"margin-left:11%;\">as_lines<\/p>\n<p style=\"margin-left:17%;\"><i>Instance method.<\/i> 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 ( <small>NB:<\/small> indistinguishable from an empty body!).<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Note: the default method gets the data via repeated <b>getline()<\/b> calls; your subclass might wish to override this.<\/p>\n<p style=\"margin-left:11%;\">as_string<\/p>\n<p style=\"margin-left:17%;\"><i>Instance method.<\/i> Return the body data as a string (slurping it into core if necessary). Best not to do this unless you\u2019re <i>sure<\/i> that the body is reasonably small! Returns empty string for an empty body, and undef on failure.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Note: the default method uses <b>print()<\/b>, which gets the data via repeated <b>read()<\/b> calls; your subclass might wish to override this.<\/p>\n<p style=\"margin-left:11%;\">binmode [ <small>ONOFF<\/small> ]<\/p>\n<p style=\"margin-left:17%;\"><i>Instance method.<\/i> With argument, flags whether or not <b>open()<\/b> should return an I\/O handle which has <b>binmode()<\/b> activated. With no argument, just returns the current value.<\/p>\n<p style=\"margin-left:11%;\">is_encoded [ <small>ONOFF<\/small> ]<\/p>\n<p style=\"margin-left:17%;\"><i>Instance method.<\/i> If set to yes, no decoding is applied on output. This flag is set by MIME::Parser, if the parser runs in <b>decode_bodies<\/b>(0) mode, so the content is handled unmodified.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"4%\">\n<p>dup<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"66%\">\n<p><i>Instance method.<\/i> Duplicate the bodyhandle.<\/p>\n<\/td>\n<td width=\"17%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\"><i>Beware:<\/i> external data in bodyhandles is <i>not<\/i> copied to new files! Changing the data in one body\u2019s data file, or purging that body, <i>will<\/i> affect its duplicate. Bodies with in-core data probably need not worry.<\/p>\n<p style=\"margin-left:11%;\">open <small>READWRITE<\/small><\/p>\n<p style=\"margin-left:17%;\"><i>Instance method, abstract.<\/i> This should do whatever is necessary to open the body for either writing (if <small>READWRITE<\/small> is &#8220;w&#8221;) or reading (if mode is &#8220;r&#8221;).<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">This method is expected to return an &#8220;I\/O handle&#8221; object on success, and undef on error. An I\/O handle can be any object that supports a small set of standard methods for reading\/writing data. See the IO::Handle class for an example.<\/p>\n<p style=\"margin-left:11%;\">path [ <small>PATH<\/small> ]<\/p>\n<p style=\"margin-left:17%;\"><i>Instance method.<\/i> If you\u2019re storing the body data externally (e.g., in a disk file), you\u2019ll want to give applications the ability to get at that data, for cleanup. This method should return the path to the data, or undef if there is none.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Where appropriate, the path <i>should<\/i> be a simple string, like a filename. With argument, sets the <small>PATH,<\/small> which should be undef if there is none.<\/p>\n<p style=\"margin-left:11%;\">print <small>FILEHANDLE<\/small><\/p>\n<p style=\"margin-left:17%;\"><i>Instance method.<\/i> Output the body data to the given filehandle, or to the currently-selected one if none is given.<\/p>\n<p style=\"margin-left:11%;\">purge<\/p>\n<p style=\"margin-left:17%;\"><i>Instance method, abstract.<\/i> Remove any data which resides external to the program (e.g., in disk files). Immediately after a <b>purge()<\/b>, the <b>path()<\/b> should return undef to indicate that the external data is no longer available.<\/p>\n<h2>SUBCLASSES <a name=\"SUBCLASSES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following built-in classes are provided:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Body Stores body When open()ed, <br \/> class: data in: returns: <br \/> \u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212 <br \/> MIME::Body::File disk file IO::Handle <br \/> MIME::Body::Scalar scalar IO::Handle <br \/> MIME::Body::InCore scalar array IO::Handle<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>MIME::Body::File<\/b> <br \/> A body class that stores the data in a disk file. Invoke the constructor as:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::File &#8220;\/path\/to\/file&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In this case, the &#8220;path()&#8221; method would return the given path, so you <i>could<\/i> say:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (defined($body\u2212>path)) { <br \/> open BODY, $body\u2212>path or die &#8220;open: $!&#8221;; <br \/> while (<BODY>) { <br \/> ### do stuff <br \/> } <br \/> close BODY; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">But you\u2019re best off not doing this.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>MIME::Body::Scalar<\/b> <br \/> A body class that stores the data in-core, in a simple scalar. Invoke the constructor as:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::Scalar $string;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A single scalar argument sets the body to that value, exactly as though you\u2019d opened for the body for writing, written the value, and closed the body again:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::Scalar &#8220;Line 1nLine 2nLine 3&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A single array reference sets the body to the result of joining all the elements of that array together:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::Scalar [&#8220;Line 1n&#8221;, <br \/> &#8220;Line 2n&#8221;, <br \/> &#8220;Line 3&#8221;];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>MIME::Body::InCore<\/b> <br \/> A body class that stores the data in-core. Invoke the constructor as:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::InCore $string; <br \/> $body = new MIME::Body::InCore $string; <br \/> $body = new MIME::Body::InCore @stringarray<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A simple scalar argument sets the body to that value, exactly as though you\u2019d opened for the body for writing, written the value, and closed the body again:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::InCore &#8220;Line 1nLine 2nLine 3&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A single array reference sets the body to the concatenation of all scalars that it holds:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$body = new MIME::Body::InCore [&#8220;Line 1n&#8221;, <br \/> &#8220;Line 2n&#8221;, <br \/> &#8220;Line 3&#8221;];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Defining your own subclasses<\/b> <br \/> So you\u2019re 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 &#8220;new_body_for(head)&#8221; method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Your &#8220;body&#8221; class must inherit from MIME::Body (or some subclass of it), and it must either provide (or inherit the default for) the following methods&#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The default inherited method <i>should suffice<\/i> for all these:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">new <br \/> binmode [ONOFF] <br \/> path<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The default inherited method <i>may suffice<\/i> for these, but perhaps there\u2019s a better implementation for your subclass.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">init ARGS&#8230; <br \/> as_lines <br \/> as_string <br \/> dup <br \/> print <br \/> purge<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The default inherited method <i>will probably not suffice<\/i> for these:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">open<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">One reason I didn\u2019t just use IO::Handle objects for message bodies was that I wanted a &#8220;body&#8221; object to be a form of completely encapsulated program-persistent storage; that is, I wanted users to be able to write code like this&#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">### Get body handle from this MIME message, and read its data: <br \/> $body = $entity\u2212>bodyhandle; <br \/> $IO = $body\u2212>open(&#8220;r&#8221;); <br \/> while (defined($_ = $IO\u2212>getline)) { <br \/> print STDOUT $_; <br \/> } <br \/> $IO\u2212>close;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8230;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).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Storing the body of each <small>MIME<\/small> message in a persistently-open IO::Handle was a possibility, but it seemed like a bad idea, considering that a single multipart <small>MIME<\/small> 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 <small>MIME<\/small> entity at a time.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">MIME::Tools<\/p>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Eryq (<i>eryq@zeegee.com<\/i>), ZeeGee Software Inc (<i>http:\/\/www.zeegee.com<\/i>). David F. Skoll (dfs@roaringpenguin.com) http:\/\/www.roaringpenguin.com<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">All rights reserved. This program is free software; you can redistribute it and\/or modify it under the same terms as Perl itself.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thanks to Achim Bohnet for suggesting that MIME::Parser not be restricted to the use of FileHandles.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212 1;<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  MIME::Body \u2212 the body of a MIME message <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3007,3759],"class_list":["post-7447","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-mimebody"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7447"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7447\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}