#!/usr/bin/perl # Demonstrate some real-world encoding fixes. # This has been tested on Perls 5.6.1 and 5.8.0. BEGIN { require v5.6.0; } use utf8; no bytes; binmode STDOUT, ':utf8' if $] >= 5.008; # suppresses a warning # Correctly encoded data (string-is-unicode bit set) print "\n\$good:\n"; $good = chr(0x03B1) . chr(0x00DF) . chr(0x044D); print $good, "\n", length($good), "\n"; # three characters # Make a copy without the string-is-unicode bit set on it # This is the kind of thing DBD::mysql returns if you put something like $good # into the database originally. binmode STDOUT, ':bytes' if $] >= 5.008; print "\n\$bad:\n"; $bad = pack("C0C*", unpack("C0C*", $good)); print $bad, "\n", length($bad), "\n"; # six bytes print "\n(\$bad eq \$good): " . (($bad eq $good) ? "yes" : "no") . "\n"; # At Perl 5.6.1, this says "yes". # At Perl 5.8.0, this says "no". # Repack the bad string into another correctly-tagged string binmode STDOUT, ':utf8' if $] >= 5.008; print "\n\$also_good:\n"; $also_good = pack("U0U*", unpack("U0U*", $bad)); print $also_good, "\n", length($also_good), "\n"; print "\n(\$bad eq \$also_good): " . (($bad eq $also_good) ? "yes" : "no") . "\n"; print "(\$good eq \$also_good): " . (($good eq $also_good) ? "yes" : "no") . "\n\n";