How do i "actually" encode the doc in UTF-8?

That depends on how you generate the document. If you create it by hand, it could look something like

use charnames ':full'; { my %escapes = ( '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;', "'" => '&apos;', ); sub xml_text { (my $s = $_[0]) =~ s/([<&])/$escapes{$1}/g; $s } sub xml_att_val { (my $s = $_[0]) =~ s/([<&"])/$escapes{$1}/g; qq{"$s"} } } my $s_7bit = "A"; my $s_8bit = "\N{LATIN CAPITAL LETTER A WITH CIRCUMFLEX}"; my $s_32bit = "\N{BLACK SPADE SUIT}";
open(my $fh, '>:encoding(UTF-8)', $qfn) or die; print($fh qq{<?xml version="1.0" encoding="UTF-8"?>\n}); print($fh qq{<foo>\n}); print($fh qq{ <bar>}, xml_text($s_7bit), qq{</bar>\n}); print($fh qq{ <bar>}, xml_text($s_8bit), qq{</bar>\n}); print($fh qq{ <bar>}, xml_text($s_32bit), qq{</bar>\n}); print($fh qq{</foo>\n});

The :encoding PerlIO layer will encode the characters as UTF-8. Without the :encoding layer, the IO system will assume the characters are already encoded (and will freak out if you pass it characters that aren't bytes).

Update: Fixed bug in code (wasn't using the handle I opened). Added the required prefix to the original code.


In reply to Re^3: Parsing extended ascii characters using XML::LibXML... by ikegami
in thread Parsing extended ascii characters using XML::LibXML... by biswanath_c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.