What is your environment? On my machine it works just fine (see below). I use this to get the relevant information:

alias ts='perl -v | grep "This is"; \ perl -e"use XML::Twig; \ print qq{XML::Twig: \$XML::Twig::VERSION\n}"; \ perl -e"use XML::Parser; \ print qq{XML::Parser: \$XML::Parser::VERSION\n}"; \ xmlwf -v;'

Note that if the last line (xmlwf... returns an error you are probably using expat 1.95.2 (xmlwf did not support the -v option at the time).

This is my test environment:</p

This is perl, v5.6.1 built for i686-linux XML::Twig: 3.09 XML::Parser: 2.31 xmlwf using expat_1.95.5

In any case this works fine for me:

#!/usr/bin/perl -w use strict; use XML::Twig; sub Node { my ($twig, $node) = @_; my $txt = $node->text (); my $id = $node->att ('id'); #print two strings individually print "$id\n"; print "$txt\n"; #print list - OK print "1) list: ", $id, " ", $txt, "\n"; #print with string concat - still OK print "2) concat: $id $txt\n"; #the concatenated string DOES match the Latin1 part my $res = "$id $txt" =~ /$txt/ ? "match" : "NO match"; print "\"$id $txt\" =~ /$txt/ => $res\n"; } package main; my $twig = XML::Twig->new (KeepEncoding => 1, TwigHandlers => {Node => + \&Node}); $twig->parse (\*DATA); __DATA__ <?xml version="1.0" encoding="ISO-8859-1"?> <Document> <Node id="50">fünfzig</Node> </Document>

A last remark: there is nothing in the XML spec that specifies what the XML processor (they don't even use the word parser) is supposed to report. It just happens that most parsers convert to UTF-8 (or probably UTF-18 in the Java world) because it makes sense. At least expat and libxml do, see The internal encoding, how and why for a good explanation. The keep_encoding option of XML::Twig is quite atypical, and it is there for the common case of all documents being in the same, known in advance (1-byte) encoding.


In reply to Re: Problems with string concatenation and encodings by mirod
in thread Problems with string concatenation and encodings by pike

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.