Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to build a real multi-level dispatch table

by dragonchild (Archbishop)
on Jan 04, 2002 at 21:40 UTC ( #136318=note: print w/ replies, xml ) Need Help??


in reply to Defense of nested ifs
in thread Why I Hate Nested If-Else blocks

Using Ovid's example above ...

sub whatFoo { if ($_[0] > 7) { return 'ONE' } else { return 'TWO' } } sub whatBarBaz { my ($bar, $baz) = @_; return 'BAR' if $bar; return 'BAZ' if $baz; return 'DEFAULT'; } sub no_op { } my %Dispatch = ( ONE => { BAR => \&firstBar, BAZ => \&firstBaz, }, TWO => { BAR => \&secondBar, BAZ => \&secondBaz, }, ); # Put the defaults in as NO-OPs, unless we already have a # default action foreach my $key (keys %Dispatch) { $Dispatch{$key}{DEFAULT} = \&no_op unless exists $Dispatch{$key}{DEFAULT}; } my $func = $Dispatch{whatFoo($foo)}{whatBarBaz($bar, $baz)}; $func->(@someArgs);
Seems pretty clear-cut to me ... Plus, it's extensible. If you have a $quz you want to put in, add your QUZ entries to the dispatch and then change whatBarBaz() to allow for QUZ.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


Comment on How to build a real multi-level dispatch table
Download Code
Replies are listed 'Best First'.
Re: How to build a real multi-level dispatch table
by George_Sherston (Vicar) on Jan 08, 2002 at 01:15 UTC
    I've been following this thread with some interest, as I just finished writing a CGI with a very unwieldy set of if / elsif / else statements, and I'm interested in how to put it on a more rational footing (posted below for anyone who wants a laugh). In simpler situations I've used a dispatch table. But here I couldn't work out how to pass (the right) arguments. As far as I can see, this is not handled explicitly above. I'd be interested in how to deal with the need (A) to pass different arguments to different subs; and (B)to pass different args to the *same* sub under different conditions ?

    My baobab-style if tree, so we can all see what I'm up against:
    if ($g{'Action'} eq 'MainPage') { MainPage($Persist, $UserName); } if (($g{'Action'} eq 'Course') or ($g{'Action'} eq 'EditCourse')) +{ $g{'CourseID'} = GetNewCourse() unless defined $g{'CourseID'}; UpdateTempData($g{'CourseID'}); EditCourse($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Create Course') { my $CourseID = GetNewCourse($UserName); UpdateTempData($CourseID); EditCourse($Persist, $CourseID, $g{'Return'}); } elsif ($g{'Action'} eq 'EditEvent') { EditEvent($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'New Venue') { my $VenueID = GetNewVenue($Persist,$UserName); EditVenue($Persist, $VenueID, $g{'Return'}); } elsif (($g{'Action'} eq 'Venue') or ($g{'Action'} eq 'EditVenue')) + { EditVenue($Persist, $g{'VenueID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Course Overview') { CourseView($Persist, $UserName); } elsif ($g{'Action'} eq 'Venue Overview') { VenueView($Persist,$UserName); } elsif ($g{'Action'} eq 'DeleteCourse') { DeleteCourse($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Delete Course') { DeleteCourseYes($Persist, $g{'CourseID'}, $g{'Return'}); if ($g{'Return'} eq 'CourseView') { CourseView($Persist, $UserName); } else { MainPage($Persist, $UserName); } } elsif ($g{'Action'} eq 'Keep Course') { if ($g{'Return'} eq 'CourseView') { CourseView($Persist, $UserName); } else { MainPage($Persist, $UserName); } } elsif ($g{'Action'} eq 'DeleteEvent') { DeleteEvent( $Persist, $g{'EventID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Delete Event') { DeleteEventYes( $Persist, $g{'EventID'}); if ($g{'Return'} eq 'CourseView') { CourseView($Persist, $UserName); } elsif ($g{'Return'} eq 'EditEvent') { EditEvent($Persist, $g{'CourseID'},0,$g{'CourseTitle'}); } else { MainPage($Persist, $UserName); } } elsif ($g{'Action'} eq 'Keep Event') { if ($g{'Return'} eq 'CourseView') { CourseView($Persist, $UserName); } elsif ($g{'Return'} eq 'EditEvent') { EditEvent($Persist, $g{'CourseID'},0,$g{'CourseTitle'}); } else { MainPage($Persist, $UserName); } } elsif ($g{'Action'} eq 'DeleteVenue') { DeleteVenue($Persist, $g{'VenueID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Delete Venue') { DeleteVenueYes($Persist, $g{'VenueID'}, $g{'Return'}); if ($g{'Return'} eq 'VenueView') { VenueView($Persist, $UserName); } else { MainPage($Persist, $UserName); } } elsif ($g{'Action'} eq 'Keep Venue') { if ($g{'Return'} eq 'VenueView') { VenueView($Persist, $UserName); } else { MainPage($Persist, $UserName); } } elsif ($g{'Action'} eq 'Save Venue') { if (my $Errors = SaveVenue($Persist, $UserName)) { EditVenue($Persist, $g{'VenueID'}, $g{'Return'}, $Errors); } else { WriteVenue(); if ($g{'Return'} eq 'ReturnToEditEvent') { SaveVenue($Persist, $UserName); EditEvent($Persist, $g{'CourseID'}, $g{'Return'},$g{'C +ourseTitle'},0); } elsif ($g{'Return'} eq 'VenueView') { SaveVenue($Persist, $UserName); VenueView($Persist, $UserName); } else { SaveVenue($Persist, $UserName); MainPage($Persist, $UserName); } } } elsif ($g{'Action'} eq 'More Keywords') { EditCourse($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Next Step') { if (my @Errors = CourseErrorCheck()) { EditCourse($Persist, $g{'CourseID'}, $g{'Return'}, \@Error +s); } elsif (my $OldTitle = TitleCheck()) { UpdateTempData($g{'CourseID'}); QueryTitle($Persist, $g{'CourseID'}, $g{'Return'}, $OldTit +le); } else { UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'}); } } elsif ($g{'Action'} eq 'New Course') { my $g = GetFromFile($g{'CourseID'},\%g); %g = %$g; DestroyTempFile($g{'CourseID'}); $g{'CourseID'} = GetNewCourse(); MakeTempFile($g{'CourseID'}); UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Change Name') { UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'More Links') { UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'}); } elsif ($g{'Action'} eq 'Another Page') { SaveUserPage($Persist, $g{'CourseID'},$g{'PageTitle'},$g{'Page +Text'},$g{'WeFormat'},$g{'CurrentPage'}); $g{'PageTitle'} = undef; $g{'PageText'} = undef; EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'},0,1); } elsif ($g{'Action'} =~ /^Edit Page (\d*)$/) { SaveUserPage($Persist, $g{'CourseID'},$g{'PageTitle'},$g{'Page +Text'},$g{'WeFormat'},$g{'CurrentPage'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'}, $g{"Pag +eID_$1"},1); } elsif ($g{'Action'} =~ /^Delete Page (\d*)$/) { UpdateTempData($g{'CourseID'}); SaveUserPage($Persist, $g{'CourseID'},$g{'PageTitle'},$g{'Page +Text'},$g{'WeFormat'},$g{'CurrentPage'}); QueryDeleteUserPage($Persist, $g{'CourseID'}, $g{'Return'},$g{ +"PageID_$1"},$g{'CurrentPage'}); } elsif ($g{'Action'} eq 'Yes, Delete Page') { DeleteUserPage($g{'PageID'}); UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'},$g{'Curr +entPage'},1); } elsif ($g{'Action'} eq 'No, Keep Page') { UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'},$g{'Curr +entPage'},1); } elsif ($g{'Action'} eq 'No, Keep Page') { UpdateTempData($g{'CourseID'}); EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'},$g{'Curr +entPage'},1); } elsif ($g{'Action'} eq 'Proceed') { SaveUserPage($Persist, $g{'CourseID'},$g{'PageTitle'},$g{'Page +Text'},$g{'WeFormat'},$g{'CurrentPage'}); UpdateTempData($g{'CourseID'}); if (my $TearSheetError = CourseInfoErrorCheck()) { EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'},0,0, +$TearSheetError); } else { EditEvent($Persist, $g{'CourseID'}, $g{'Return'},$g{'Cours +eTitle'},0); } } elsif ($g{'Action'} eq 'Save Course') { SaveUserPage($Persist, $g{'CourseID'},$g{'PageTitle'},$g{'Page +Text'},$g{'WeFormat'},$g{'CurrentPage'}); UpdateTempData($g{'CourseID'}); if (my $TearSheetError = CourseInfoErrorCheck()) { EditCourseInfo($Persist, $g{'CourseID'}, $g{'Return'},0,0, +$TearSheetError); } else { SaveCourse($Persist, $g{'CourseID'}, $g{'Return'}); if ($g{'Return'} eq 'CourseView') { CourseView($Persist, $UserName); } else { MainPage($Persist, $UserName); } } } elsif ($g{'Action'} eq 'Make Venue') { my $VenueID = GetNewVenue($Persist,$UserName); EditVenue($Persist, $VenueID,'ReturnToEditEvent',0,$g{'CourseI +D'},$g{'CourseTitle'}); } elsif ($g{'Action'} eq 'Add Event') { if (my $Errors = CheckEvent($g{'CourseID'})) { EditEvent($Persist, $g{'CourseID'}, $g{'Return'},$g{'Cours +eTitle'},$Errors); } else { AddEvent($Persist, $g{'CourseID'}, $g{'CourseTitle'}); EditEvent($Persist, $g{'CourseID'}, $g{'Return'},$g{'Cours +eTitle'},0); } } elsif ($g{'Action'} eq 'Save All') { my $Errors = CheckEvent($g{'CourseID'}); my $DateError = 0; my $VenueError = 0; my $DuplicateError = 0; if ($Errors) { $DateError = 1 if grep {$_ eq 'Date'} @$Errors; $VenueError = 1 if grep {$_ eq 'Venue'} @$Errors; $DuplicateError = 1 if grep {$_ eq 'Duplicate'} @$Errors; } if ($DateError + $VenueError == 1) { EditEvent($Persist, $g{'CourseID'}, $g{'Return'},$g{'Cours +eTitle'},$Errors); } else { AddEvent($Persist, $g{'CourseID'}, $g{'CourseTitle'}) unle +ss ($DuplicateError == 1 or $DateError + $VenueError == 2); if (stat "$filepath/tempfiles/$g{'CourseID'}") { UpdateTempData($g{'CourseID'}); SaveCourse($Persist, $g{'CourseID'}, $g{'Return'}); } if (defined $g{'Return'} and $g{'Return'} eq 'CourseView') + { CourseView($Persist, $UserName); } else { MainPage($Persist, $UserName); } } } else { MainPage($Persist, $UserName, $UserCookie); }


    George Sherston

      That is what closures are for:

      my %actions= ( MainPage => sub { MainPage( $Persis, $UserName ) }, Course => sub { EditCourse( $Persist, def(\$g{CourseID},sub{GetNewCourse()}), $g{Return} ) }, EditCourse => 'Course', 'Create Course' => sub { EditCourse( $Persist, GetNewCourse($UserName), $g{Return} ) }, EditEvent => sub { EditEvent( $Persist, $g{CourseID}, $g{Return} ) }, 'New Venue' => sub { EditVenue( $Persist, GetNewVenue($Persist,$UserName), $g{Return} ) }, Venue => sub { EditVenue( $Persist, $g{'VenueID'}, $g{'Return'} ) }, EditVenue => 'Venue', 'Delete Course' => \&DeleteCoursePage, 'Keep Course' => sub { if ($g{'Return'} eq 'CourseView') { CourseView($Persist, $UserName); } else { MainPage($Persist, $UserName); } }, # ... ); # ... my $action= $actions{ $g{Action} }; if( ! $action ) { MainPage( $Persist, $UserName, $UserCookie ); } else { $action= $actions{$action} if ! ref($action); $action->(); }
      where you add more subroutines as needed to keep the hash declaration from being too complex.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://136318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (15)
As of 2015-07-30 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls