Body.pm.html   [plain text]


<HTML>
<HEAD>
  <TITLE>MIME::Body</TITLE>
</HEAD>
<BODY 
       bgcolor="#FFFFFF" link="#CC3366" vlink="#993366" alink="#FF6666">
<FONT FACE="sans-serif" SIZE=-1><A HREF="http://www.zeegee.com" TARGET="_top"><IMG SRC="icons/zeegee.gif" ALT="ZeeGee Software" ALIGN="RIGHT" BORDER="0"></A><A NAME="__TOP__"><H1>MIME::Body</H1>
</A>
<P><B>This module is <FONT COLOR="#990000">BETA</FONT> code, which means that the interfaces are fairly stable BUT it has not been out in the community long enough to guarantee much testing. Use with caution! Please report any errors back to <A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A> as soon as you can.</B><UL>
<LI> <A HREF="#NAME">NAME</A>
<LI> <A HREF="#SYNOPSIS">SYNOPSIS</A>
<UL>
<LI> <A HREF="#Obtaining_bodies">Obtaining bodies</A>
<LI> <A HREF="#Opening_closing_and_using_IO_handles">Opening, closing, and using IO handles</A>
<LI> <A HREF="#Other_I_O">Other I/O</A>
<LI> <A HREF="#Working_directly_with_paths_to_underlying_files">Working directly with paths to underlying files</A>
</UL>
<LI> <A HREF="#DESCRIPTION">DESCRIPTION</A>
<LI> <A HREF="#PUBLIC_INTERFACE">PUBLIC INTERFACE</A>
<LI> <A HREF="#SUBCLASSES">SUBCLASSES</A>
<UL>
<LI> <A HREF="#MIME_Body_File">MIME::Body::File</A>
<LI> <A HREF="#MIME_Body_Scalar">MIME::Body::Scalar</A>
<LI> <A HREF="#MIME_Body_InCore">MIME::Body::InCore</A>
<LI> <A HREF="#Defining_your_own_subclasses">Defining your own subclasses</A>
</UL>
<LI> <A HREF="#NOTES">NOTES</A>
<LI> <A HREF="#AUTHOR">AUTHOR</A>
<LI> <A HREF="#VERSION">VERSION</A>
</UL>
</A>

<P><HR>
<A NAME="NAME"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> NAME</H2></A>


<P>MIME::Body - the body of a MIME message



<P><HR>
<A NAME="SYNOPSIS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> SYNOPSIS</H2></A>


<P>Before reading further, you should see <A HREF="../MIME/Tools.pm.html">MIME::Tools</A> to make sure that 
you understand where this module fits into the grand scheme of things.
Go on, do it now.  I'll wait.


<P>Ready?  Ok...



<P><HR>
<A NAME="Obtaining_bodies"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Obtaining bodies</H3></A>

<FONT SIZE=3 FACE="courier"><PRE>
   ### Get the bodyhandle of a MIME::Entity object:
   $body = $entity-&gt;bodyhandle;
</PRE></FONT>
<FONT SIZE=3 FACE="courier"><PRE>
   ### Create a body which stores data in a disk file:
   $body = new MIME::Body::File &quot;/path/to/file&quot;;
</PRE></FONT>
<FONT SIZE=3 FACE="courier"><PRE>
   ### Create a body which stores data in an in-core array:
   $body = new MIME::Body::InCore \@strings;
</PRE></FONT>


<P><HR>
<A NAME="Opening_closing_and_using_IO_handles"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Opening, closing, and using IO handles</H3></A>

<FONT SIZE=3 FACE="courier"><PRE>
   ### Write data to the body:
   $IO = $body-&gt;open(&quot;w&quot;)      || die &quot;open body: $!&quot;;
   $IO-&gt;print($message);
   $IO-&gt;close                  || die &quot;close I/O handle: $!&quot;;
   
   ### Read data from the body (in this case, line by line):
   $IO = $body-&gt;open(&quot;r&quot;)      || die &quot;open body: $!&quot;;
   while (defined($_ = $IO-&gt;getline)) {
       ### do stuff
   }
   $IO-&gt;close                  || die &quot;close I/O handle: $!&quot;;
    
</PRE></FONT>


<P><HR>
<A NAME="Other_I_O"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Other I/O</H3></A>

<FONT SIZE=3 FACE="courier"><PRE>
   ### Dump the ENCODED body data to a filehandle:
   $body-&gt;print(\*STDOUT);
       
   ### Slurp all the UNENCODED data in, and put it in a scalar:
   $string = $body-&gt;as_string;
   
   ### Slurp all the UNENCODED data in, and put it in an array of lines:
   @lines = $body-&gt;as_lines;
</PRE></FONT>


<P><HR>
<A NAME="Working_directly_with_paths_to_underlying_files"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Working directly with paths to underlying files</H3></A>

<FONT SIZE=3 FACE="courier"><PRE>
   ### Where's the data?
   if (defined($body-&gt;path)) {   ### data is on disk:
       print &quot;data is stored externally, in &quot;, $body-&gt;path;
   }
   else {                        ### data is in core:
       print &quot;data is already in core, and is...\n&quot;, $body-&gt;as_string;
   }
     
   ### Get rid of anything on disk:
   $body-&gt;purge;
</PRE></FONT>


<P><HR>
<A NAME="DESCRIPTION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> DESCRIPTION</H2></A>


<P>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.


<P>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 &quot;body&quot; object
usually looks like this:



<OL>
<P><LI>
<P><B>Body object is created by a MIME::Parser during parsing.</B>
It's at this point that the actual MIME::Body subclass is chosen,
and new() 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><LI>
<P><B>Data is written to the body</B> (usually by the MIME parser) like this:
The body is opened for writing, via <CODE>open(&quot;w&quot;)</CODE>.  This will trash any 
previous contents, and return an &quot;I/O handle&quot; opened for writing.  
Data is written to this I/O handle, via print().
Then the I/O handle is closed, via close().

<P><LI>
<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 <CODE>open(&quot;r&quot;)</CODE>.
This will return an &quot;I/O handle&quot; opened for reading.
Data is read from the I/O handle, via read(), getline(), or getlines().
Then the I/O handle is closed, via close().

<P><LI>
<P><B>Body object is destructed.</B>

</OL>


<P>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).


<P>In any case, once a MIME::Body has been created, you ask to open it
for reading or writing, which gets you an &quot;i/o handle&quot;: you then use 
the same mechanisms for reading from or writing to that handle, no matter 
what class it is.


<P>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><HR>
<A NAME="PUBLIC_INTERFACE"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> PUBLIC INTERFACE</H2></A>



<DL>
<P><DT><B><A NAME="item:new">new ARGS...</A></B></DT>
<DD>
<I>Class method, constructor.</I>
Create a new body.  Any ARGS are sent to init().

<P><DT><B><A NAME="item:init">init ARGS...</A></B></DT>
<DD>
<I>Instance method, abstract, initiallizer.</I>
This is called automatically by <CODE>new()</CODE>, with the arguments given
to <CODE>new()</CODE>.  The arguments are optional, and entirely up to the
subclass.  The default method does nothing,

<P><DT><B><A NAME="item:as_lines">as_lines</A></B></DT>
<DD>
<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 (NB: indistinguishable from an empty body!).


<P>Note: the default method gets the data via
repeated getline() calls; your subclass might wish to override this.

<P><DT><B><A NAME="item:as_string">as_string</A></B></DT>
<DD>
<I>Instance method.</I>
Return the body data as a string (slurping it into core if necessary).  
Best not to do this unless you're <I>sure</I> that the body is reasonably small!
Returns empty string for an empty body, and undef on failure.


<P>Note: the default method uses print(), which gets the data via
repeated read() calls; your subclass might wish to override this.

<P><DT><B><A NAME="item:binmode">binmode [ONOFF]</A></B></DT>
<DD>
<I>Instance method.</I>
With argument, flags whether or not open() should return an I/O handle
which has binmode() activated.  With no argument, just returns the
current value.  

<P><DT><B><A NAME="item:dup">dup</A></B></DT>
<DD>
<I>Instance method.</I>
Duplicate the bodyhandle.


<P><I>Beware:</I> external data in bodyhandles is <I>not</I> copied to new files!  
Changing the data in one body's data file, or purging that body,
<I>will</I> affect its duplicate.  Bodies with in-core data probably need
not worry.

<P><DT><B><A NAME="item:open">open READWRITE</A></B></DT>
<DD>
<I>Instance method, abstract.</I>
This should do whatever is necessary to open the body for either
writing (if READWRITE is &quot;w&quot;) or reading (if mode is &quot;r&quot;).


<P>This method is expected to return an &quot;I/O handle&quot; 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><DT><B><A NAME="item:path">path [PATH]</A></B></DT>
<DD>
<I>Instance method.</I>
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, or undef if there is none.


<P>Where appropriate, the path <I>should</I> be a simple string, like a filename.
With argument, sets the PATH, which should be undef if there is none.

<P><DT><B><A NAME="item:print">print FILEHANDLE</A></B></DT>
<DD>
<I>Instance method.</I>
Output the body data to the given filehandle, or to the currently-selected 
one if none is given.

<P><DT><B><A NAME="item:purge">purge</A></B></DT>
<DD>
<I>Instance method, abstract.</I>
Remove any data which resides external to the program (e.g., in disk files).
Immediately after a purge(), the path() should return undef to indicate
that the external data is no longer available.

</DL>



<P><HR>
<A NAME="SUBCLASSES"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> SUBCLASSES</H2></A>


<P>The following built-in classes are provided:

<FONT SIZE=3 FACE="courier"><PRE>
   Body                 Stores body     When open()ed,
   class:               data in:        returns:    
   --------------------------------------------------------
   MIME::Body::File     disk file       IO::Handle   
   MIME::Body::Scalar   scalar          IO::Scalar  
   MIME::Body::InCore   scalar array    IO::ScalarArray
</PRE></FONT>


<P><HR>
<A NAME="MIME_Body_File"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> MIME::Body::File</H3></A>


<P>A body class that stores the data in a disk file.  
The I/O handle is a wrapped filehandle.  Invoke the constructor as:

<FONT SIZE=3 FACE="courier"><PRE>
    $body = new MIME::Body::File &quot;/path/to/file&quot;;
</PRE></FONT>

<P>In this case, the <CODE>path()</CODE> method would return the given path,
so you <I>could</I> say:

<FONT SIZE=3 FACE="courier"><PRE>
    if (defined($body-&gt;path)) {
	open BODY, $body-&gt;path or die &quot;open: $!&quot;;
	while (&lt;BODY&gt;) {
	    ### do stuff
        }
	close BODY;
    }
</PRE></FONT>

<P>But you're best off not doing this.



<P><HR>
<A NAME="MIME_Body_Scalar"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> MIME::Body::Scalar</H3></A>


<P>A body class that stores the data in-core, in a simple scalar.
Invoke the constructor as:

<FONT SIZE=3 FACE="courier"><PRE>
    $body = new MIME::Body::Scalar \$string;
</PRE></FONT>

<P>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:

<FONT SIZE=3 FACE="courier"><PRE>
    $body = new MIME::Body::Scalar &quot;Line 1\nLine 2\nLine 3&quot;;
</PRE></FONT>

<P>A single array reference sets the body to the result of joining all the
elements of that array together:

<FONT SIZE=3 FACE="courier"><PRE>
    $body = new MIME::Body::Scalar [&quot;Line 1\n&quot;,
                                    &quot;Line 2\n&quot;,
                                    &quot;Line 3&quot;];
</PRE></FONT>

<P>Uses <B>IO::Scalar</B> as the I/O handle.



<P><HR>
<A NAME="MIME_Body_InCore"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> MIME::Body::InCore</H3></A>


<P>A body class that stores the data in-core.
Invoke the constructor as:

<FONT SIZE=3 FACE="courier"><PRE>
    $body = new MIME::Body::InCore \$string;
    $body = new MIME::Body::InCore  $string;
    $body = new MIME::Body::InCore \@stringarray
</PRE></FONT>

<P>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 &quot;Line 1\nLine 2\nLine 3&quot;;


<P>A single array reference sets the body to the concatenation of all
scalars that it holds:

<FONT SIZE=3 FACE="courier"><PRE>
    $body = new MIME::Body::InCore [&quot;Line 1\n&quot;,
                                    &quot;Line 2\n&quot;,
                                    &quot;Line 3&quot;];
</PRE></FONT>

<P>Uses <B>IO::ScalarArray</B> as the I/O handle.



<P><HR>
<A NAME="Defining_your_own_subclasses"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Defining your own subclasses</H3></A>


<P>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 <CODE>new_body_for(head)</CODE> method.


<P>Your &quot;body&quot; class must inherit from MIME::Body (or some subclass of it),
and it must either provide (or inherit the default for) the following 
methods...


<P>The default inherited method <I>should suffice</I> for all these:

<FONT SIZE=3 FACE="courier"><PRE>
    new                       
    binmode [ONOFF]           
    path
</PRE></FONT>

<P>The default inherited method <I>may suffice</I> for these, but perhaps 
there's a better implementation for your subclass.                       

<FONT SIZE=3 FACE="courier"><PRE>
    init ARGS...              
    as_lines                  
    as_string                 
    dup                       
    print                     
    purge 
</PRE></FONT>

<P>The default inherited method <I>will probably not suffice</I> for these:

<FONT SIZE=3 FACE="courier"><PRE>
    open                      
</PRE></FONT>


<P><HR>
<A NAME="NOTES"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> NOTES</H2></A>


<P>One reason I didn't just use FileHandle or IO::Handle objects for message
bodies was that I wanted a &quot;body&quot; object to be a form of completely
encapsulated program-persistent storage; that is, I wanted users
to be able to write code like this...

<FONT SIZE=3 FACE="courier"><PRE>
   ### Get body handle from this MIME message, and read its data:
   $body = $entity-&gt;bodyhandle;
   $IO = $body-&gt;open(&quot;r&quot;);
   while (defined($_ = $IO-&gt;getline)) {
       print STDOUT $_;
   }
   $IO-&gt;close;
</PRE></FONT>

<P>...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>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.



<P><HR>
<A NAME="AUTHOR"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> AUTHOR</H2></A>


<P>Eryq (<I><FILE><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></FILE></I>), ZeeGee Software Inc (<I><FILE><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></FILE></I>).


<P>All rights reserved.  This program is free software; you can redistribute 
it and/or modify it under the same terms as Perl itself.


<P>Thanks to Achim Bohnet for suggesting that MIME::Parser not be restricted
to the use of FileHandles.



<P><HR>
<A NAME="VERSION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> VERSION</H2></A>


<P>$Revision: 1.1 $ $Date: 2004/04/09 17:04:45 $

<P><HR>
<ADDRESS><FONT SIZE=-1>
Generated Wed Jan 17 01:57:51 2001 by cvu_pod2html
</FONT></ADDRESS>
</FONT></BODY>
</HTML>