in reply to Re: Write to file, after manipulation
in thread Write to file, after manipulation

ibanix wrote:
sub bot_command { my $command = shift(@_); join_channel($command) if ($command =~ /join/); part_channel($command) if ($command =~ /part/); msg_user($command) if ($command =~ /msg/); .... default($command); }
Ugh!
sub bot_command { local $_ = shift; /join/ ? &join_channel : /part/ ? &part_channel : /msg/ ? &msg_user : &default }
I'm still trying to figure out how a dispatch table works
Like this:
sub join_channel { ... } sub part_channel { ... } sub msg_user { ... } my %dispatch = ( join => \&join_channel, part => \&part_channel, msg => \&msg_user, ); sub bot_command { my $cmd = shift; $dispatch{$cmd} ? &{$dispatch{$cmd}} : &default }

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.

Replies are listed 'Best First'.
Re: Re: Re: Write to file, after manipulation
by jdporter (Paladin) on Jan 29, 2003 at 20:06 UTC
    But then, another way is to let perl manage the dispatch table for you.
    Like so:
    sub BotCmd::join_channel { ... } sub BotCmd::part_channel { ... } sub BotCmd::msg_user { ... } sub BotCmd::default { ... } my %method_map = ( # cmd: method: join => 'join_channel', part => 'part_channel', msg => 'msg_user', ); sub bot_command { my $cmd = shift; my $method = $method_map{ $cmd || 'default' }; BotCmd->$method(@_); }

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Re: Re: Write to file, after manipulation
by ibanix (Hermit) on Jan 28, 2003 at 22:01 UTC
    Ugh!

    I said I liked it -- not that you had to. TIMTOWDI.

    Thanks for the dispatch table introduction! I think I may be using that method in my code soon.

    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;