in reply to convert UTF-8 in nested data structures

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? } }

Replies are listed 'Best First'.
Re^2: convert UTF-8 in nested data structures
by slayven (Pilgrim) on Oct 31, 2005 at 15:28 UTC
    thanks for the length. I really should form a habit of using it.
    What about escaping < and >?
    They didn't appear in my data yet but you're right, they're likely to be taken care of.

    And finally, you're absolutely right about the scalar parameters. Thanks again.


    --
    trust in bash
    but tie your camel
Re^2: convert UTF-8 in nested data structures
by slayven (Pilgrim) on Nov 07, 2005 at 13:00 UTC
    Just to complete the sub to adapt on real world data.
    return unless defined $val;
    helps on undefined values.
    -- slayven
            trust in bash
            but tie your camel