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/&/&/g; $val =~ s/</</g; $val =~ s/>/>/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |