# "The Unicode Bug" is perlunicode's own name for it: without the
# unicode_strings feature, whether é is a word character -- or has an upper
# case -- depends on how perl happens to be storing the string. `use v5.12` or
# later turns the feature on. A one-liner never has it.
use v5.36;
use utf8;

binmode STDOUT, ':encoding(UTF-8)';

sub yn { $_[0] ? 'yes' : 'no' }    # no signature: a failed match in an argument list passes nothing

my $one_byte = "\x{E9}";    # é, which fits in perl's one-byte-per-character form
utf8::downgrade($one_byte);
my $utf8_form = "\x{E9}";   # the same character, stored internally as UTF-8
utf8::upgrade($utf8_form);

{
    no feature 'unicode_strings';
    say "--- without the unicode_strings feature ---";
    say "  the same string?          ", yn($one_byte eq $utf8_form);
    say "  /\\w/ on the one-byte form  ", yn($one_byte =~ /\w/);
    say "  /\\w/ on the UTF-8 form     ", yn($utf8_form =~ /\w/);
    say "  uc of the one-byte form    ", uc $one_byte;
    say "  uc of the UTF-8 form       ", uc $utf8_form;
}
say "";

say "--- with use v5.36, which turns it on ---";
say "  /\\w/ on the one-byte form  ", yn($one_byte =~ /\w/);
say "  /\\w/ on the UTF-8 form     ", yn($utf8_form =~ /\w/);
say "  uc of the one-byte form    ", uc $one_byte;
say "  uc of the UTF-8 form       ", uc $utf8_form;
say "";

say "--- so \\w, \\d, \\s and /i mean Unicode, and /a takes it back ---";
say "  'Kraków' =~ /^\\w+\$/        ", yn('Kraków' =~ /^\w+$/), "   with /a: ", yn('Kraków' =~ /^\w+$/a);
my $three = "\x{663}";      # ARABIC-INDIC DIGIT THREE
say "  '٣' =~ /\\d/                ", yn($three =~ /\d/), "   with /a: ", yn($three =~ /\d/a);
{
    my $warned = 0;
    local $SIG{__WARN__} = sub { $warned = 1 };
    my $number = $three + 0;
    say "  '٣' + 0                    $number    warned: ", yn($warned);
}
my $nbsp = "\x{A0}";        # NO-BREAK SPACE
say "  NBSP =~ /\\s/               ", yn($nbsp =~ /\s/), "   with /a: ", yn($nbsp =~ /\s/a);
my $kelvin = "\x{212A}";    # KELVIN SIGN
say "  KELVIN SIGN =~ /k/i        ", yn($kelvin =~ /k/i), "   with /aa: ", yn($kelvin =~ /k/aai);
say "";

say "--- a one-liner has no use v5.36: -e has the bug, -E does not ---";
my $code = 'print "\x{E9}" =~ /\w/ ? "yes\n" : "no\n"';
for my $switch ('-e', '-E') {
    open my $run, '-|', $^X, $switch, $code or die $!;
    my $answer = <$run>;
    close $run;
    print "  perl $switch '$code'   $answer";
}
