WordDecoder.pm.html   [plain text]


<HTML>
<HEAD>
  <TITLE>MIME::WordDecoder</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::WordDecoder</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>
<LI> <A HREF="#DESCRIPTION">DESCRIPTION</A>
<LI> <A HREF="#PUBLIC_INTERFACE">PUBLIC INTERFACE</A>
<LI> <A HREF="#SUBCLASSES">SUBCLASSES</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::WordDecoder - decode RFC-1522 encoded words to a local representation



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


<P>See <A HREF="../MIME/Words.pm.html">MIME::Words</A> for the basics of encoded words.
See <A HREF="#DESCRIPTION">DESCRIPTION</A> for how this class works.

<FONT SIZE=3 FACE="courier"><PRE>
    use MIME::WordDecoder;
     
    
    ### Get the default word-decoder (used by unmime()):
    $wd = default MIME::WordDecoder;
      
    ### Get a word-decoder which maps to ISO-8859-1 (Latin1):
    $wd = supported MIME::WordDecoder &quot;ISO-8859-1&quot;;
    
       
    ### Decode a MIME string (e.g., into Latin1) via the default decoder:
    $str = $wd-&gt;decode('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= &lt;keld&gt;');
      
    ### Decode a string using the default decoder, non-OO style:
    $str = unmime('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= &lt;keld&gt;');
 
</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>A MIME::WordDecoder consists, fundamentally, of a hash which maps
a character set name (US-ASCII, ISO-8859-1, etc.) to a subroutine which 
knows how to take bytes in that character set and turn them into 
the target string representation.  Ideally, this target representation 
would be Unicode, but we don't want to overspecify the translation 
that takes place: if you want to convert MIME strings directly to Big5, 
that's your own decision.


<P>The subroutine will be invoked with two arguments: DATA (the data in
the given character set), and CHARSET (the upcased character set name).


<P>For example:

<FONT SIZE=3 FACE="courier"><PRE>
    ### Keep 7-bit characters as-is, convert 8-bit characters to '#':
    sub keep7bit {  
	local $_ = shift;
	tr/\x00-\x7F/#/c;
	$_;
    }
</PRE></FONT>

<P>Here's a decoder which uses that:

<FONT SIZE=3 FACE="courier"><PRE>
   ### Construct a decoder:
   $wd = MIME::WordDecoder-&gt;new({'US-ASCII'   =&gt; &quot;KEEP&quot;,   ### sub { $_[0] }
                                 'ISO-8859-1' =&gt; \&amp;keep7bit,
                                 'ISO-8859-2' =&gt; \&amp;keep7bit,
                                 'Big5'       =&gt; &quot;WARN&quot;,
                                 '*'          =&gt; &quot;DIE&quot;});
         
   ### Convert some MIME text to a pure ASCII string...   
   $ascii = $wd-&gt;decode('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= &lt;keld&gt;');
      
   ### ...which will now hold: &quot;To: Keld J#rn Simonsen &lt;keld&gt;&quot;
</PRE></FONT>


<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:default">default [DECODER]</A></B></DT>
<DD>
<I>Class method.</I>
Get/set the default DECODER object.

<P><DT><B><A NAME="item:supported">supported CHARSET, [DECODER]</A></B></DT>
<DD>
<I>Class method.</I>
If just CHARSET is given, returns a decoder object which maps
data into that character set (the character set is forced to
all-uppercase).  

<FONT SIZE=3 FACE="courier"><PRE>
    $wd = supported MIME::WordDecoder &quot;ISO-8859-1&quot;;
</PRE></FONT>

<P>If DECODER is given, installs such an object:

<FONT SIZE=3 FACE="courier"><PRE>
    MIME::WordDecoder-&gt;supported(&quot;ISO-8859-1&quot; =&gt; 
				 (new MIME::WordDecoder::ISO_8859 &quot;1&quot;));
</PRE></FONT>

<P>You should not override this method.

<P><DT><B><A NAME="item:new">new [\@HANDLERS]</A></B></DT>
<DD>
<I>Class method, constructor.</I>
If \@HANDLERS is given, then @HANDLERS is passed to handler()
to initiallize the internal map.

<P><DT><B><A NAME="item:handler">handler CHARSET=&gt;\&amp;SUBREF, ...</A></B></DT>
<DD>
<I>Instance method.</I>
Set the handler SUBREF for a given CHARSET, for as many pairs
as you care to supply.


<P>When performing the translation of a MIME-encoded string, a
given SUBREF will be invoked when translating a block of text
in character set CHARSET.  The subroutine will be invoked with 
the following arguments:

<FONT SIZE=3 FACE="courier"><PRE>
    DATA    - the data in the given character set.
    CHARSET - the upcased character set name, which may prove useful
              if you are using the same SUBREF for multiple CHARSETs.
    DECODER - the decoder itself, if it contains configuration information
              that your handler function needs.
</PRE></FONT>

<P>For example:

<FONT SIZE=3 FACE="courier"><PRE>
    $wd = new MIME::WordDecoder;
    $wd-&gt;handler('US-ASCII'   =&gt; &quot;KEEP&quot;);
    $wd-&gt;handler('ISO-8859-1' =&gt; \&amp;handle_latin1,
		 'ISO-8859-2' =&gt; \&amp;handle_latin1,
		 '*'          =&gt; &quot;DIE&quot;);
</PRE></FONT>

<P>Notice that, much as with %SIG, the SUBREF can also be taken from
a set of special keywords:

<FONT SIZE=3 FACE="courier"><PRE>
   KEEP     Pass data through unchanged.
   IGNORE   Ignore data in this character set, without warning.
   WARN     Ignore data in this character set, with warning.
   DIE      Fatal exception with &quot;can't handle character set&quot; message.
</PRE></FONT>

<P>The subroutine for the special CHARSET of 'raw' is used for raw
(non-MIME-encoded) text, which is supposed to be US-ASCII.  
The handler for 'raw' defaults to whatever was specified for 'US-ASCII'
at the time of construction.


<P>The subroutine for the special CHARSET of '*' is used for any 
unrecognized character set.  The default action for '*' is WARN.

<P><DT><B><A NAME="item:decode">decode STRING</A></B></DT>
<DD>
<I>Instance method.</I>
Decode a STRING which might contain MIME-encoded components into a
local representation (e.g., UTF-8, etc.).

<P><DT><B><A NAME="item:unmime">unmime STRING</A></B></DT>
<DD>
<I>Function, exported.</I>
Decode the given STRING using the default() decoder.
See <A HREF="#item:default">default()</A>.

</DL>



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



<DL>
<P><DT><B><A NAME="item:MIME_WordDecoder_ISO_8859">MIME::WordDecoder::ISO_8859</A></B></DT>
<DD>
A simple decoder which keeps US-ASCII and the 7-bit characters
of ISO-8859 character sets and UTF8, and also keeps 8-bit
characters from the indicated character set.

<FONT SIZE=3 FACE="courier"><PRE>
    ### Construct:
    $wd = new MIME::WordDecoder::ISO_8859 2;    ### ISO-8859-2
       
    ### What to translate unknown characters to (can also use empty):
    ### Default is &quot;?&quot;.
    $wd-&gt;unknown(&quot;?&quot;);
    
    ### Collapse runs of unknown characters to a single unknown()?
    ### Default is false.
    $wd-&gt;collapse(1);
</PRE></FONT>

<P>According to <B>http://czyborra.com/charsets/iso8859.html</B> 
(ca. November 2000):


<P>ISO 8859 is a full series of 10 (and soon even more) standardized
multilingual single-byte coded (8bit) graphic character sets for
writing in alphabetic languages:

<P><OL><LI>Latin1 (West European) <LI>Latin2 (East European) <LI>Latin3 (South European) <LI>Latin4 (North European) <LI>Cyrillic <LI>Arabic <LI>Greek <LI>Hebrew <LI>Latin5 (Turkish) <LI>Latin6 (Nordic) </OL>
<P>The ISO 8859 charsets are not even remotely as complete as the truly
great Unicode but they have been around and usable for quite a while
(first registered Internet charsets for use with MIME) and have
already offered a major improvement over the plain 7bit US-ASCII.


<P>Characters 0 to 127 are always identical with US-ASCII and the
positions 128 to 159 hold some less used control characters: the
so-called C1 set from ISO 6429.

<P><DT><B><A NAME="item:MIME_WordDecoder_US_ASCII">MIME::WordDecoder::US_ASCII</A></B></DT>
<DD>
A subclass of the ISO-8859-1 decoder which discards 8-bit characters.  
You're probably better off using ISO-8859-1.

</DL>



<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><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:58:44 2001 by cvu_pod2html
</FONT></ADDRESS>
</FONT></BODY>
</HTML>