#!/usr/bin/env bash
# With no encoding layer on a filehandle, perl writes each string in whichever
# internal form it happens to have: every character up to U+00FF as one byte,
# unless the string holds a character above U+00FF -- then the whole string
# goes out as UTF-8, with a "Wide character" warning. So the same é is one byte
# or two depending on its neighbours.
#
# Every command's output goes through `hex`, a dump written in perl so it
# prints the same on macOS and Linux (od and hexdump format differently).
set -u
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cd "$work" || exit 1

hex() { perl -0777 -ne 'print join(" ", unpack "(H2)*", $_), "\n"'; }
say() { printf '$ %s\n' "$*"; eval "$*"; echo; }

say "perl -e 'print \"caf\x{E9}\n\"' | hex"
say "perl -e 'print \"caf\x{E9} \x{2192} ok\n\"' 2>warnings.txt | hex; cat warnings.txt"
say "perl -e 'print \"caf\x{E9}\n\", \"caf\x{E9} \x{2192}\n\"' 2>/dev/null | hex"

say "perl -CS -e 'print \"caf\x{E9} \x{2192} ok\n\"' 2>warnings.txt | hex; cat warnings.txt"
say "perl -e 'binmode STDOUT, \":encoding(UTF-8)\"; print \"caf\x{E9}\n\"' | hex"
say "perl -e 'use open qw(:std :encoding(UTF-8)); print \"caf\x{E9} \x{2192} ok\n\"' 2>warnings.txt | hex; cat warnings.txt"
