in reply to simpliest way to add "language" to my app
Something that's relatively simple is to have a hash, with the keys being the command, and the values being anonymous subroutines.
my %command= ( shutdown => sub { print "Shutting down...\n"; exit 0; }, lock_user => sub { my ($user) = @_; # Insert code to lock $user here. }, unlock_user => sub { my ($user) = @_; # Insert code here to unlock $user }, show => sub { # Insert show code here }, lock => sub { $_ = shift; if ($command{lock_$_}) { $command{lock_$_}->(@_); } else { die "Don't know how to lock $_\n"; } }, unlock => sub { $_ = shift; if ($command{unlock_$_}) { $command{unlock_$_}->(@_); } else { die "Don't know how to unlock $_\n"; } }, help => sub {# update per L~R's suggestion: print "Current commands: "; print join "\n", sort { $a cmp $b } keys %command; }, ); my ($cmd, @args) = @ARGV; # Or however you get them. $cmd = lc $cmd; # Force lowercase per L~R's suggestion. my $sub = $command{$cmd}; $sub = $command{help} if not defined $sub; $sub->(@args);
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: simpliest way to add "language" to my app
by Limbic~Region (Chancellor) on Oct 05, 2003 at 04:03 UTC | |
by jonadab (Parson) on Oct 05, 2003 at 04:20 UTC | |
by smackdab (Pilgrim) on Oct 05, 2003 at 04:38 UTC |