elsif (ref $ref eq '' && $ref) {
should probably be
elsif (ref $ref eq '' && length $ref) {
to cover '0' and 0.

What about escaping < and >?

I find it odd that you return the converted value, since you're converting in place.

Also, your variable $ref is misnamed, since it's not necessarily a ref.

I've also added support for scalar refs.

use Encode; sub digConvert { our $val; local *val = \$_[0]; if (ref $val eq 'HASH') { foreach (keys %$val) { digConvert($val->{$_}); } } elsif (ref $val eq 'ARRAY') { foreach my $i (0 .. $#{$val}) { digConvert($val->[$i]); } } elsif (ref $val eq 'SCALAR') { digConvert($$val); } elsif (!ref $val && length $val) { # don't upset XML parser # problably more to come $val =~ s/&/&amp;/g; $val =~ s/</&lt;/g; $val =~ s/>/&gt;/g; $val = Encode::encode_utf8($val); } else { ### something I missed? } }

In reply to Re: convert UTF-8 in nested data structures by ikegami
in thread convert UTF-8 in nested data structures by slayven

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.