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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (5)
    As of 2014-11-23 20:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (134 votes), past polls