in reply to Trying to simulate a CLI using hashes...
%command_list = (
quit => \&end_program,
greeting => \&greeting,
time => \&systime,
help => \&help,
log => \&which_log # this one is new
);
sub which_log {
my $arg = shift;
return start_logs() if ($arg eq "start");
return stop_logs() if ($arg eq "stop");
return log_status(); # default behavior
}
$command = <STDIN>; # assume $command="log start"
@cl = split / /,$command;
my $func = shift @cl; # first CLI argument is hash key
if (defined($command_list{$func})) {
$command_list{$func}->(@cl); # passes rest of CLI args to handler
} else {
print "invalid command\n";
}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Trying to simulate a CLI using hashes...
by thoth (Novice) on Jan 22, 2002 at 20:12 UTC |