6.20 略語にマッチするパターン

send, abort, list, editといったユーザコマンドがある。これらのコマンドの全ての文字を入力しなくても、コマンドが実行されるようにしたい。

  • 優先順位を付ける方法
chomp($answer = <>);
if      ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" }
elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" }
  • Text::Abbrevモジュールを使用する方法
use Text::Abbrev;
$href = abbrev qw(send abort list edit);
for (print "Action: "; <>; print "Action: ") {
  chomp();
  my $action = $href->{ lc($_) };
  print "Action is $action\n";
}