{
    "mode": "perldoc",
    "parameter": "MIME::Body",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/MIME%3A%3ABody/json",
    "generated": "2026-06-16T10:13:04Z",
    "synopsis": "Before reading further, you should see MIME::Tools to make sure that you understand where this\nmodule fits into the grand scheme of things. Go on, do it now. I'll wait.\nReady? Ok...",
    "sections": {
        "NAME": {
            "content": "MIME::Body - the body of a MIME message\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "Before reading further, you should see MIME::Tools to make sure that you understand where this\nmodule fits into the grand scheme of things. Go on, do it now. I'll wait.\n\nReady? Ok...\n",
            "subsections": [
                {
                    "name": "Obtaining bodies",
                    "content": "### Get the bodyhandle of a MIME::Entity object:\n$body = $entity->bodyhandle;\n\n### Create a body which stores data in a disk file:\n$body = new MIME::Body::File \"/path/to/file\";\n\n### Create a body which stores data in an in-core array:\n$body = new MIME::Body::InCore \\@strings;\n"
                },
                {
                    "name": "Opening, closing, and using IO handles",
                    "content": "### Write data to the body:\n$IO = $body->open(\"w\")      || die \"open body: $!\";\n$IO->print($message);\n$IO->close                  || die \"close I/O handle: $!\";\n\n### Read data from the body (in this case, line by line):\n$IO = $body->open(\"r\")      || die \"open body: $!\";\nwhile (defined($ = $IO->getline)) {\n### do stuff\n}\n$IO->close                  || die \"close I/O handle: $!\";\n\nOther I/O\n### Dump the ENCODED body data to a filehandle:\n$body->print(\\*STDOUT);\n\n### Slurp all the UNENCODED data in, and put it in a scalar:\n$string = $body->asstring;\n\n### Slurp all the UNENCODED data in, and put it in an array of lines:\n@lines = $body->aslines;\n"
                },
                {
                    "name": "Working directly with paths to underlying files",
                    "content": "### Where's the data?\nif (defined($body->path)) {   ### data is on disk:\nprint \"data is stored externally, in \", $body->path;\n}\nelse {                        ### data is in core:\nprint \"data is already in core, and is...\\n\", $body->asstring;\n}\n\n### Get rid of anything on disk:\n$body->purge;\n"
                }
            ]
        },
        "DESCRIPTION": {
            "content": "MIME messages can be very long (e.g., tar files, MPEGs, etc.) or very short (short textual\nnotes, as in ordinary mail). Long messages are best stored in files, while short ones are\nperhaps best stored in core.\n\nThis class is an attempt to define a common interface for objects which contain message data,\nregardless of how the data is physically stored. The lifespan of a \"body\" object usually looks\nlike this:\n\n1.  Body object is created by a MIME::Parser during parsing. It's at this point that the actual\nMIME::Body subclass is chosen, and new() is invoked. (For example: if the body data is going\nto a file, then it is at this point that the class MIME::Body::File, and the filename, is\nchosen).\n\n2.  Data is written to the body (usually by the MIME parser) like this: The body is opened for\nwriting, via \"open(\"w\")\". This will trash any previous contents, and return an \"I/O handle\"\nopened for writing. Data is written to this I/O handle, via print(). Then the I/O handle is\nclosed, via close().\n\n3.  Data is read from the body (usually by the user application) like this: The body is opened\nfor reading by a user application, via \"open(\"r\")\". This will return an \"I/O handle\" opened\nfor reading. Data is read from the I/O handle, via read(), getline(), or getlines(). Then\nthe I/O handle is closed, via close().\n\n4.  Body object is destructed.\n\nYou can write your own subclasses, as long as they follow the interface described below.\nImplementers of subclasses should assume that steps 2 and 3 may be repeated any number of times,\nand in different orders (e.g., 1-2-2-3-2-3-3-3-3-3-2-4).\n\nIn any case, once a MIME::Body has been created, you ask to open it for reading or writing,\nwhich gets you an \"i/o handle\": you then use the same mechanisms for reading from or writing to\nthat handle, no matter what class it is.\n\nBeware: unless you know for certain what kind of body you have, you should *not* assume that the\nbody has an underlying filehandle.\n",
            "subsections": []
        },
        "PUBLIC INTERFACE": {
            "content": "new ARGS...\n*Class method, constructor.* Create a new body. Any ARGS are sent to init().\n\ninit ARGS...\n*Instance method, abstract, initiallizer.* This is called automatically by \"new()\", with the\narguments given to \"new()\". The arguments are optional, and entirely up to the subclass. The\ndefault method does nothing,\n\naslines\n*Instance method.* Return the contents of the body as an array of lines (each terminated by\na newline, with the possible exception of the final one). Returns empty on failure (NB:\nindistinguishable from an empty body!).\n\nNote: the default method gets the data via repeated getline() calls; your subclass might\nwish to override this.\n\nasstring\n*Instance method.* Return the body data as a string (slurping it into core if necessary).\nBest not to do this unless you're *sure* that the body is reasonably small! Returns empty\nstring for an empty body, and undef on failure.\n\nNote: the default method uses print(), which gets the data via repeated read() calls; your\nsubclass might wish to override this.\n\nbinmode [ONOFF]\n*Instance method.* With argument, flags whether or not open() should return an I/O handle\nwhich has binmode() activated. With no argument, just returns the current value.\n\nisencoded [ONOFF]\n*Instance method.* If set to yes, no decoding is applied on output. This flag is set by\nMIME::Parser, if the parser runs in decodebodies(0) mode, so the content is handled\nunmodified.\n\ndup *Instance method.* Duplicate the bodyhandle.\n\n*Beware:* external data in bodyhandles is *not* copied to new files! Changing the data in\none body's data file, or purging that body, *will* affect its duplicate. Bodies with in-core\ndata probably need not worry.\n\nopen READWRITE\n*Instance method, abstract.* This should do whatever is necessary to open the body for\neither writing (if READWRITE is \"w\") or reading (if mode is \"r\").\n\nThis method is expected to return an \"I/O handle\" object on success, and undef on error. An\nI/O handle can be any object that supports a small set of standard methods for\nreading/writing data. See the IO::Handle class for an example.\n\npath [PATH]\n*Instance method.* If you're storing the body data externally (e.g., in a disk file), you'll\nwant to give applications the ability to get at that data, for cleanup. This method should\nreturn the path to the data, or undef if there is none.\n\nWhere appropriate, the path *should* be a simple string, like a filename. With argument,\nsets the PATH, which should be undef if there is none.\n\nprint FILEHANDLE\n*Instance method.* Output the body data to the given filehandle, or to the\ncurrently-selected one if none is given.\n\npurge\n*Instance method, abstract.* Remove any data which resides external to the program (e.g., in\ndisk files). Immediately after a purge(), the path() should return undef to indicate that\nthe external data is no longer available.\n",
            "subsections": []
        },
        "SUBCLASSES": {
            "content": "The following built-in classes are provided:\n\nBody                 Stores body     When open()ed,\nclass:               data in:        returns:\n--------------------------------------------------------\nMIME::Body::File     disk file       IO::Handle\nMIME::Body::Scalar   scalar          IO::Handle\nMIME::Body::InCore   scalar array    IO::Handle\n\nMIME::Body::File\nA body class that stores the data in a disk file. Invoke the constructor as:\n\n$body = new MIME::Body::File \"/path/to/file\";\n\nIn this case, the \"path()\" method would return the given path, so you *could* say:\n\nif (defined($body->path)) {\nopen BODY, $body->path or die \"open: $!\";\nwhile (<BODY>) {\n### do stuff\n}\nclose BODY;\n}\n\nBut you're best off not doing this.\n\nMIME::Body::Scalar\nA body class that stores the data in-core, in a simple scalar. Invoke the constructor as:\n\n$body = new MIME::Body::Scalar \\$string;\n\nA single scalar argument sets the body to that value, exactly as though you'd opened for the\nbody for writing, written the value, and closed the body again:\n\n$body = new MIME::Body::Scalar \"Line 1\\nLine 2\\nLine 3\";\n\nA single array reference sets the body to the result of joining all the elements of that array\ntogether:\n\n$body = new MIME::Body::Scalar [\"Line 1\\n\",\n\"Line 2\\n\",\n\"Line 3\"];\n\nMIME::Body::InCore\nA body class that stores the data in-core. Invoke the constructor as:\n\n$body = new MIME::Body::InCore \\$string;\n$body = new MIME::Body::InCore  $string;\n$body = new MIME::Body::InCore \\@stringarray\n\nA simple scalar argument sets the body to that value, exactly as though you'd opened for the\nbody for writing, written the value, and closed the body again:\n\n$body = new MIME::Body::InCore \"Line 1\\nLine 2\\nLine 3\";\n\nA single array reference sets the body to the concatenation of all scalars that it holds:\n\n$body = new MIME::Body::InCore [\"Line 1\\n\",\n\"Line 2\\n\",\n\"Line 3\"];\n",
            "subsections": [
                {
                    "name": "Defining your own subclasses",
                    "content": "So you're not happy with files and scalar-arrays? No problem: just define your own MIME::Body\nsubclass, and make a subclass of MIME::Parser or MIME::ParserBase which returns an instance of\nyour body class whenever appropriate in the \"newbodyfor(head)\" method.\n\nYour \"body\" class must inherit from MIME::Body (or some subclass of it), and it must either\nprovide (or inherit the default for) the following methods...\n\nThe default inherited method *should suffice* for all these:\n\nnew\nbinmode [ONOFF]\npath\n\nThe default inherited method *may suffice* for these, but perhaps there's a better\nimplementation for your subclass.\n\ninit ARGS...\naslines\nasstring\ndup\nprint\npurge\n\nThe default inherited method *will probably not suffice* for these:\n\nopen\n"
                }
            ]
        },
        "NOTES": {
            "content": "One reason I didn't just use IO::Handle objects for message bodies was that I wanted a \"body\"\nobject to be a form of completely encapsulated program-persistent storage; that is, I wanted\nusers to be able to write code like this...\n\n### Get body handle from this MIME message, and read its data:\n$body = $entity->bodyhandle;\n$IO = $body->open(\"r\");\nwhile (defined($ = $IO->getline)) {\nprint STDOUT $;\n}\n$IO->close;\n\n...without requiring that they know anything more about how the $body object is actually storing\nits data (disk file, scalar variable, array variable, or whatever).\n\nStoring the body of each MIME message in a persistently-open IO::Handle was a possibility, but\nit seemed like a bad idea, considering that a single multipart MIME message could easily suck up\nall the available file descriptors on some systems. This risk increases if the user application\nis processing more than one MIME entity at a time.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "MIME::Tools\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Eryq (eryq@zeegee.com), ZeeGee Software Inc (http://www.zeegee.com). David F. Skoll\n(dfs@roaringpenguin.com) http://www.roaringpenguin.com\n\nAll rights reserved. This program is free software; you can redistribute it and/or modify it\nunder the same terms as Perl itself.\n\nThanks to Achim Bohnet for suggesting that MIME::Parser not be restricted to the use of\nFileHandles.\n\n#------------------------------ 1;\n",
            "subsections": []
        }
    },
    "summary": "MIME::Body - the body of a MIME message",
    "flags": [],
    "examples": [],
    "see_also": []
}