# A string literal is bytes unless `use utf8` says the source file is UTF-8.
# This file IS saved as UTF-8, and the same letters below make two different
# strings depending on whether the pragma is in scope. `use utf8` is lexical,
# so both halves fit in one file.
use v5.36;

my $bytes       = do { no utf8;  "zażółć" };
my $chars       = do { use utf8; "zażółć" };
my $upper_bytes = do { no utf8;  "ZAŻÓŁĆ" };

sub bytes_of ($s)       { join ' ', map { sprintf '%02x', ord } split //, $s }
sub code_points_of ($s) { join ' ', map { sprintf 'U+%04X', ord } split //, $s }

say "--- one literal, two strings ---";
say "no utf8:   length ", length($bytes), "  ", bytes_of($bytes);
say "use utf8:  length ", length($chars), "   ", code_points_of($chars);
say "";

say "--- string functions work on what they were given ---";
say "uc  no utf8:   ", bytes_of(uc $bytes), "   only z and a changed";
say "uc  use utf8:  ", code_points_of(uc $chars);
say "lc  no utf8:   ", bytes_of(lc $upper_bytes), "   c5 c3 c4 lower-cased as Latin-1 letters";
say "rev no utf8:   ", bytes_of(scalar reverse $bytes), "   the bytes of each letter reversed too";
say "rev use utf8:  ", code_points_of(scalar reverse $chars);
say "";

say "--- and printing them ---";
my $city_bytes = do { no utf8;  "Kraków" };
my $city_chars = do { use utf8; "Kraków" };
print "no layer, no utf8:        $city_bytes   (bytes in, the same bytes out)\n";
binmode STDOUT, ':encoding(UTF-8)';
print "UTF-8 layer, use utf8:    $city_chars\n";
print "UTF-8 layer, no utf8:     $city_bytes   (each byte encoded a second time)\n";
