Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

(tye)Re: How to build a real multi-level dispatch table

by tye (Sage)
on Jan 08, 2002 at 02:00 UTC ( #136971=note: print w/ replies, xml ) Need Help??

in reply to Re: How to build a real multi-level dispatch table
in thread Why I Hate Nested If-Else blocks

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")

Comment on (tye)Re: How to build a real multi-level dispatch table
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2016-02-10 19:02 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (354 votes), past polls