# Perl grep since 'grep -P' is terrible.

local usage pattern modifiers

usage="$(
cat <<EOF
usage: $0 [-option ...] [--] pattern [file ...]

options:
  -i    ignore case
  -m    ^ and $ match the start and the end of a line
  -s    . matches newline
  -x    ignore whitespace and comments
EOF
)"

while getopts ':igmxe::' opt; do
  case "$opt" in
    (i) modifiers="${modifiers}i" ;;
    (m) modifiers="${modifiers}m" ;;
    (x) modifiers="${modifiers}x" ;;
    (e) modifiers="${modifiers}e" ;;
    (:)
      print "$0: option requires an argument: $OPTARG" >&2
      print "$usage" >&2
      return 1
    ;;
    ([?])
      print "$0: unknown option: $OPTARG" >&2
      print "$usage" >&2
      return 1
    ;;
  esac
done
shift $(( $OPTIND - 1 ))

if (( $# < 1 )); then
  print "$usage" >&2
  return 1
fi

pattern="$1"
shift

perl -n -l -e "print if m/${pattern//\//\\/}/${modifiers}" "$@"