time # with no args, command means "show time"
time arg [...] # with args, it means "set time"
####
while () {
my ( $cmd, @args ) = split;
if ( exists( $command{$cmd} )) {
$command{$cmd}->( @args );
else {
$command{default}->();
}
}
####
my %command = (
time => sub {
if ( @_ == 0 ) {
print "time is ", scalar localtime, $/;
} else {
print "let's pretend the time is @_\n";
}
},
date => sub {
if ( @_ == 0 ) {
print "You don't have a date for tonight\n";
} else {
print "Okay, we'll see if @_ will go out with you\n";
}
},
# ... and so on (updated to remove undeclared array)
);