#!/usr/bin/perl
use strict;
use warnings;
use Encode qw( _utf8_on );
my $smiley = "\xE2\x98\xBA";
# prints a smiley on my UTF-8 enabled terminal
print $smiley . "\n";
# also prints a smiley, but issues "Wide character in print" warning first.
_utf8_on($smiley);
print $smiley . "\n";
####
#!/usr/bin/perl
use strict;
use warnings;
# utf8_concat.plx -- concat utf8 and non-utf8 strings
use Encode qw( _utf8_on );
printf("%-20s%s\n", "STRING", "OCTETS");
my $foo = "foo";
print_octets('"foo"', $foo);
my $packed_num = pack('N', 128);
print_octets("packed num:", $packed_num);
print_octets("non-utf8 concat:", $foo . $packed_num);
_utf8_on($foo);
print_octets("utf8 concat:", $foo . $packed_num);
sub print_octets {
my ($label, $string) = @_;
printf("%-20s", $label);
my @octets = unpack('C*', $string);
print "@octets\n";
}
__END__
Output:
STRING OCTETS
"foo" 102 111 111
packed num: 0 0 0 128
non-utf8 concat: 102 111 111 0 0 0 128
utf8 concat: 102 111 111 0 0 0 194 128
####
print $utf8; # UTF8 flag is on
print $non_utf8; # UTF8 flag is off
# same output as above but issues warning if $non_utf8 isn't valid UTF-8
print $utf8 . $non_utf8;
# produces translated output after manual intervention
my $translated_to_utf8 = Encode::decode("iso-8859-1", $non_utf8);
print $utf8 . $translated_to_utf8;