Perl Best Practices [Electronic resources]

Damian Conway

نسخه متنی -صفحه : 317/ 160
نمايش فراداده

10.13. Simple Prompting

Always prompt for interactive input .

There are few things more frustrating than firing up a program and then sitting there waiting for it to complete its task, only to realize after a few minutes that it's actually been just sitting there too, silently waiting for you to start interacting with it:


# The quit command is case-insensitive and may be abbreviated...
Readonly my $QUIT => qr/\A q(?:uit)? \z/ixms;
# No command entered yet...
my $cmd = $EMPTY_STR;
# Until the q[uit] command is entered...
CMD:
while ($cmd !~ $QUIT) {
# Get the next command...
$cmd = <>;
last CMD if not defined $cmd;

# Clean it up and run it...
chomp $cmd;
execute($cmd)
or carp "Unknown command: $cmd";
}

Interactive programs should

always prompt for interaction whenever they're being run interactively:


# Until the q[uit] command is entered...

CMD: while ($cmd !~ $QUIT) {

# Prompt if we're running interactively...
if (is_interactive( )) { print get_prompt_str( ); }
# Get the next command...
$cmd = <>; last CMD if not defined $cmd;
# Clean it up and run it...
chomp $cmd; execute($cmd) or carp "Unknown command: $cmd"; }