# usage: perl_line_filter <cmd> ...
#
# run the perl command cmd on each line before printing it
perl_line_filter() {
    cmd="$1"
    shift

    # can't use -pe because it uses <> which treats the arguments as files
    perl -e "use strict; use warnings; while (<STDIN>) { ${cmd}; print; }" "$@"
}

# usage: simple_cpp <FIRST> <SECOND> ... -- <replacement-for-first> ...
#
# replaces each FIRST (on word boundaries) with <replacement-for-first>
# like a simple cpp replacement
simple_cpp() {
  # shellcheck disable=SC2016
  cmd='my $i = 0;'

  for x; do
    shift

    if test "${x}" = x--; then
      break
    fi

    cmd="${cmd} s/\\b${x}\\b/\$ARGV[\$i]/g; \$i++;"
  done

  perl_line_filter "${cmd}" -- "$@"
}
