Update:
This behavior is correct as per the source code.
(Sure wish there was an explanatory blurb explaining that XML::Twig::Elt->new() removes the '#CDATA' key in an attribute hashref. C'est la vie.)

Thank you haukex and Anon Monk for your explanation and workarounds!


Good afternoon!

I am experiencing a rather unexpected issue creating new elements with XML::Twig's XML::Twig::Elt->new() method where the '#CDATA' key in my attribute hashref gets undefined after passing it to new().

e.g.:

my $attributes = $sibling_element->atts(); $attributes->{'#CDATA'} = 1; # '#CDATA' attribute is defined here my $elt1 = XML::Twig::Elt->new('present', $attributes, 'This is in CDA +TA'); # '#CDATA' attribute is undefined here my $elt2 = XML::Twig::Elt->new('missing', $attributes, 'This is not in + CDATA');

Is this supposed to happen? I did not see anything mentioning this in the CPAN docs...

Thank you for your time.

Perl info:

perl -v This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x +64-multi-thread
XML::Twig info: 3.52

Sample code:
#!/usr/bin/perl use 5.026; use strict; use warnings; use XML::Twig; { open (my $OFILE, '>:utf8', 'file.out.xml') or die "$!\n$^E"; my $t = XML::Twig->new( twig_handlers => { '/keys/key' => sub { makeDuplicates(@_, $OFILE); 1; }, }, output_encoding => 'utf-8', pretty_print => 'indented', ); $t->safe_parse(\*DATA); if ( $@ ) { die "Error occured in XML data\n$@"; } close $OFILE; } sub makeDuplicates { my ($_twig, $key_element, $OFILE) = @_; my $text = $key_element->text(); my $attributes = $key_element->atts(); $attributes->{'#CDATA'} = 1; # '#CDATA' attribute is defined here my $lc_element = XML::Twig::Elt->new('lc_key', $attributes, lc $text +); # '#CDATA' attribute is undefined here my $uc_element = XML::Twig::Elt->new('uc_key', $attributes, uc $text +); $lc_element->paste(after => $key_element); $uc_element->paste(after => $key_element); $_twig->flush($OFILE); return; } __DATA__ <?xml version="1.0" encoding="utf-8"?> <keys> <key order='01'><![CDATA[One hen]]></key> <key order='02'><![CDATA[Two ducks]]></key> <key order='03'><![CDATA[Three squawking geese]]></key> <key order='04'><![CDATA[Four limerick oysters]]></key> <key order='05'><![CDATA[Five corpulent porpoises]]></key> <key order='06'><![CDATA[Six pair of Don Alverzo's tweezers]]></key> <key order='07'><![CDATA[Seven thousand Macedonians in full battle a +rray]]></key> <key order='08'><![CDATA[Eight brass monkeys from the ancient sacred + crypts of Egypt]]></key> <key order='09'><![CDATA[Nine apathetic, sympathetic, diabetic old m +en on roller skates, with a marked propensity towards procrastination + and sloth]]></key> <key order='10'><![CDATA[Ten lyrical, spherical, diabolical denizens + of the deep who stalk about the corners of the cove all at the same +time.]]></key> </keys>

Desired output:


Actual output:


In reply to [SOLVED] XML::Twig - '#CDATA' attribute in hashref disappears after first call to XML::Twig::Elt->new() by ateague

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.