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/&/&/g; $val =~ s/</</g; $val =~ s/>/>/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 | |
|
Re^2: convert UTF-8 in nested data structures
by slayven (Pilgrim) on Nov 07, 2005 at 13:00 UTC |