MIME::WordDecoder - decode RFC-1522 encoded words to a local representation
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 = MIME::WordDecoder->for("ISO-8859-1");
### Decode a MIME string (e.g., into Latin1) via the default decoder: $str = $wd->decode('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld>');
### Decode a string using the default decoder, non-OO style: $str = unmime('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld>');
The subroutine will be invoked with two arguments: DATA (the data in the given character set), and CHARSET (the upcased character set name).
For example:
### Keep 7-bit characters as-is, convert 8-bit characters to '#': sub keep7bit { local $_ = shift; tr/\x00-\x7F/#/c; $_; }
Here's a decoder which uses that:
### Construct a decoder: $wd = MIME::WordDecoder->new({'US-ASCII' => "KEEP", ### sub { $_[0] } 'ISO-8859-1' => \&keep7bit, 'ISO-8859-2' => \&keep7bit, 'Big5' => "WARN", '*' => "DIE"});
### Convert some MIME text to a pure ASCII string... $ascii = $wd->decode('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld>');
### ...which will now hold: "To: Keld J#rn Simonsen <keld>"
Note: as of 6.x, this is initiallized to a decoder for the default charset in this locale --- not for ISO-8859-1.
$wd = MIME::WordDecoder->for("ISO-8859-1") || die "unsupported";
If DECODER is given, installs such an object, which then overrides whatever for() would normally return.
MIME::WordDecoder->for("ISO-8859-1" => (new MIME::WordDecoder::ISO_8859 "1"));
You should not override this method.
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:
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.
For example:
package MyDecoder;
use base qw(MIME::WordDecoder);
sub new { my $class = shift; my $self = $class->SUPER::new();
$self->_handler('US-ASCII' => "KEEP"); $self->_handler('ISO-8859-1' => \&handle_latin1, 'ISO-8859-2' => \&handle_latin2, '*' => "DIE"); }
Notice that, much as with %SIG, the SUBREF can also be taken from a set of special keywords:
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 "can't handle character set" message.
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.
The subroutine for the special CHARSET of '*' is used for any unrecognized character set. The default action for '*' is WARN.
### Constructs a decoder which maps to cp1250: $wd = new MIME::WordDecoder::Unicode "cp1250";
### Decode anything into cp1250 (there's no slash-o, so you get "Jrn"): $str = $wd->decode('To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld>');
Unrecognized characters are simply omitted (sorry, but that's the way Unicode::Map works).
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.
### Construct: $wd = new MIME::WordDecoder::ISO_8859 2; ### ISO-8859-2
### What to translate unknown characters to (can also use empty): ### Default is "?". $wd->unknown("?");
### Collapse runs of unknown characters to a single unknown()? ### Default is false. $wd->collapse(1);
According to http://czyborra.com/charsets/iso8859.html (ca. November 2000):
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:
1. Latin1 (West European) 2. Latin2 (East European) 3. Latin3 (South European) 4. Latin4 (North European) 5. Cyrillic 6. Arabic 7. Greek 8. Hebrew 9. Latin5 (Turkish) 10. Latin6 (Nordic)
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.
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.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |