man-pages/man3/Text::Iconv.3pm.html
2021-03-31 01:06:50 +01:00

259 lines
6.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of Iconv</TITLE>
</HEAD><BODY>
<H1>Iconv</H1>
Section: User Contributed Perl Documentation (3pm)<BR>Updated: 2019-10-27<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
Text::Iconv - Perl interface to iconv() codeset conversion function
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
use Text::Iconv;
$converter = Text::Iconv-&gt;new(&quot;fromcode&quot;, &quot;tocode&quot;);
$converted = $converter-&gt;convert(&quot;Text to convert&quot;);
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The <B>Text::Iconv</B> module provides a Perl interface to the <B>iconv()</B>
function as defined by the Single <FONT SIZE="-1">UNIX</FONT> Specification.
<P>
The <B>convert()</B> method converts the encoding of characters in the input
string from the <I>fromcode</I> codeset to the <I>tocode</I> codeset, and
returns the result.
<P>
Settings of <I>fromcode</I> and <I>tocode</I> and their permitted combinations
are implementation-dependent. Valid values are specified in the
system documentation; the <B><A HREF="/cgi-bin/man/man2html?1+iconv">iconv</A></B>(1) utility should also provide a <B>-l</B>
option that lists all supported codesets.
<A NAME="lbAE">&nbsp;</A>
<H3>Utility methods</H3>
<B>Text::Iconv</B> objects also provide the following methods:
<P>
<B>retval()</B> returns the return value of the underlying <B>iconv()</B> function
for the last conversion; according to the Single <FONT SIZE="-1">UNIX</FONT> Specification,
this value indicates ``the number of non-identical conversions
performed.'' Note, however, that iconv implementations vary widely in
the interpretation of this specification.
<P>
This method can be called after calling <B>convert()</B>, e.g.:
<P>
<PRE>
$result = $converter-&gt;convert(&quot;lorem ipsum dolor sit amet&quot;);
$retval = $converter-&gt;retval;
</PRE>
<P>
When called before the first call to <B>convert()</B>, or if an error occured
during the conversion, <B>retval()</B> returns <B>undef</B>.
<P>
<B>get_attr()</B>: This method is only available with <FONT SIZE="-1">GNU</FONT> libiconv, otherwise
it throws an exception. The <B>get_attr()</B> method allows you to query
various attributes which influence the behavior of <B>convert()</B>. The
currently supported attributes are <I>trivialp</I>, <I>transliterate</I>, and
<I>discard_ilseq</I>, e.g.:
<P>
<PRE>
$state = $converter-&gt;get_attr(&quot;transliterate&quot;);
</PRE>
<P>
See <B><A HREF="/cgi-bin/man/man2html?3+iconvctl">iconvctl</A></B>(3) for details. To ensure portability to other iconv
implementations you should first check for the availability of this
method using <B>eval {}</B>, e.g.:
<P>
<PRE>
eval { $conv-&gt;get_attr(&quot;trivialp&quot;) };
if ($@)
{
# get_attr() is not available
}
else
{
# get_attr() is available
}
</PRE>
<P>
This method should be considered experimental.
<P>
<B>set_attr()</B>: This method is only available with <FONT SIZE="-1">GNU</FONT> libiconv, otherwise
it throws an exception. The <B>set_attr()</B> method allows you to set
various attributes which influence the behavior of <B>convert()</B>. The
currently supported attributes are <I>transliterate</I> and
<I>discard_ilseq</I>, e.g.:
<P>
<PRE>
$state = $converter-&gt;set_attr(&quot;transliterate&quot;);
</PRE>
<P>
See <B><A HREF="/cgi-bin/man/man2html?3+iconvctl">iconvctl</A></B>(3) for details. To ensure portability to other iconv
implementations you should first check for the availability of this
method using <B>eval {}</B>, cf. the description of <B>set_attr()</B> above.
<P>
This method should be considered experimental.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
If the conversion can't be initialized an exception is raised (using
<B>croak()</B>).
<A NAME="lbAG">&nbsp;</A>
<H3>Handling of conversion errors</H3>
<I>Text::Iconv</I> provides a class attribute <B>raise_error</B> and a
corresponding class method for setting and getting its value. The
handling of errors during conversion depends on the setting of this
attribute. If <B>raise_error</B> is set to a true value, an exception is
raised; otherwise, the <B>convert()</B> method only returns <B>undef</B>. By
default <B>raise_error</B> is false. Example usage:
<P>
<PRE>
Text::Iconv-&gt;<A HREF="/cgi-bin/man/man2html?1+raise_error">raise_error</A>(1); # Conversion errors raise exceptions
Text::Iconv-&gt;raise_error(0); # Conversion errors return undef
$a = Text::Iconv-&gt;raise_error(); # Get current setting
</PRE>
<A NAME="lbAH">&nbsp;</A>
<H3>Per-object handling of conversion errors</H3>
As an experimental feature, <I>Text::Iconv</I> also provides an instance
attribute <B>raise_error</B> and a corresponding method for setting and
getting its value. If <B>raise_error</B> is <B>undef</B>, the class-wide
settings apply. If <B>raise_error</B> is 1 or 0 (true or false), the
object settings override the class-wide settings.
<P>
Consult <B><A HREF="/cgi-bin/man/man2html?3+iconv">iconv</A></B>(3) for details on errors that might occur.
<A NAME="lbAI">&nbsp;</A>
<H3>Conversion of <B>undef</B></H3>
Converting <B>undef</B>, e.g.,
<P>
<PRE>
$converted = $converter-&gt;convert(undef);
</PRE>
<P>
always returns <B>undef</B>. This is not considered an error.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
The supported codesets, their names, the supported conversions, and
the quality of the conversions are all system-dependent.
<A NAME="lbAK">&nbsp;</A>
<H2>AUTHOR</H2>
Michael Piotrowski &lt;<A HREF="mailto:mxp@dynalabs.de">mxp@dynalabs.de</A>&gt;
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+iconv">iconv</A></B>(1), <B><A HREF="/cgi-bin/man/man2html?3+iconv">iconv</A></B>(3)
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="4"><A HREF="#lbAE">Utility methods</A><DD>
</DL>
<DT id="5"><A HREF="#lbAF">ERRORS</A><DD>
<DL>
<DT id="6"><A HREF="#lbAG">Handling of conversion errors</A><DD>
<DT id="7"><A HREF="#lbAH">Per-object handling of conversion errors</A><DD>
<DT id="8"><A HREF="#lbAI">Conversion of <B>undef</B></A><DD>
</DL>
<DT id="9"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="10"><A HREF="#lbAK">AUTHOR</A><DD>
<DT id="11"><A HREF="#lbAL">SEE ALSO</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:58 GMT, March 31, 2021
</BODY>
</HTML>