C
ccc31807
In a web application that I maintain, the main logic contains several
dozen elsif statements nested to several levels. The tests aren't all
the same, although in the code sample I've posted below they are the
same. I can maintain this fairly easily but I'd like to redo the logic
as it seems unwieldy and ugly to me.
This is a database application, and each branch typically calls two
procedures, the first running an SQL statement and the second a
display routine.
Is there a Perl alternative to this logic? If so, how could this be
rewritten in Perl? In general, what are alternatives to a whole bunch
of elsif statements nested to several levels?
Thanks, CC.
-------code sample------------------------
elsif ($subpage =~ /ResetPW/)
{
my $email_hash = SQL::reset_password($foruser);
CONSOLE::successful_email("$email_hash->{fullname}",
"$email_hash->{email1},$email_hash->{email2},$email_hash->{email3}",
'(e-mail address removed)', '', 'Your MUPD password has been reset',
"Dear $email_hash->{fullname}: Yoursite.org password has been reset to
********'. Please use the contact form if you have any questions about
this. Thank you.");
HTML::control_button($sesskey, 'Administer Security');
}
elsif ($subpage =~ /ChangeRole/)
{
SQL::change_role($foruser);
HTML::control_button($sesskey, 'Administer Security');
}
#-----------calendar (events) logic-----------------
elsif ($subpage =~ /DeleteEvent/)
{
SQL::delete_event($foruser);
HTML::control_button($sesskey, 'Manage Events');
}
elsif ($subpage =~ /EditEvent/)
{
HTML::control_button($sesskey, 'Manage Events');
HTML::manage_event_form($sesskey, $foruser);
}
elsif ($subpage =~ /UpdateEvent/)
{
SQL::update_event($calid, $event, $description, $place,
$eventdate, $type, $uniform, $required, $display, $comments);
HTML::control_button($sesskey, 'Manage Events');
}
elsif ($subpage =~ /DisplayEvents/)
{
my $events_ref = SQL::get_events();
HTML::display_events($events_ref);
}
#-----------news items logic-----------------
elsif ($subpage eq 'InsertNews')
{
SQL::insert_news($head, $first, $body, $priority,
$display, $comments, $username);
my $msg = qq(
HEADLINE: $head
FIRST: $first
BODY: $body
COMMENTS: $comments
USERNAME: $username
);
SITE::notify_admin('News Article Added',$msg);
HTML::control_button($sesskey, 'Add News');
}
elsif ($subpage =~ /DeleteNews/)
{
SQL::delete_news($foruser);
HTML::control_button($sesskey, 'Manage News');
}
elsif ($subpage =~ /EditNews/)
{
HTML::manage_news_form($sesskey, $foruser);
HTML::control_button($sesskey, 'Manage News');
}
elsif ($subpage =~ /UpdateNews/)
{
SQL::update_news($newsid, $head, $first, $body, $priority,
$display, $comments);
HTML::control_button($sesskey, 'Manage News');
}
elsif ($subpage =~ /DisplayNews/)
{
my $news_ref = SQL::get_news();
HTML::display_news($news_ref);
}
dozen elsif statements nested to several levels. The tests aren't all
the same, although in the code sample I've posted below they are the
same. I can maintain this fairly easily but I'd like to redo the logic
as it seems unwieldy and ugly to me.
This is a database application, and each branch typically calls two
procedures, the first running an SQL statement and the second a
display routine.
Is there a Perl alternative to this logic? If so, how could this be
rewritten in Perl? In general, what are alternatives to a whole bunch
of elsif statements nested to several levels?
Thanks, CC.
-------code sample------------------------
elsif ($subpage =~ /ResetPW/)
{
my $email_hash = SQL::reset_password($foruser);
CONSOLE::successful_email("$email_hash->{fullname}",
"$email_hash->{email1},$email_hash->{email2},$email_hash->{email3}",
'(e-mail address removed)', '', 'Your MUPD password has been reset',
"Dear $email_hash->{fullname}: Yoursite.org password has been reset to
********'. Please use the contact form if you have any questions about
this. Thank you.");
HTML::control_button($sesskey, 'Administer Security');
}
elsif ($subpage =~ /ChangeRole/)
{
SQL::change_role($foruser);
HTML::control_button($sesskey, 'Administer Security');
}
#-----------calendar (events) logic-----------------
elsif ($subpage =~ /DeleteEvent/)
{
SQL::delete_event($foruser);
HTML::control_button($sesskey, 'Manage Events');
}
elsif ($subpage =~ /EditEvent/)
{
HTML::control_button($sesskey, 'Manage Events');
HTML::manage_event_form($sesskey, $foruser);
}
elsif ($subpage =~ /UpdateEvent/)
{
SQL::update_event($calid, $event, $description, $place,
$eventdate, $type, $uniform, $required, $display, $comments);
HTML::control_button($sesskey, 'Manage Events');
}
elsif ($subpage =~ /DisplayEvents/)
{
my $events_ref = SQL::get_events();
HTML::display_events($events_ref);
}
#-----------news items logic-----------------
elsif ($subpage eq 'InsertNews')
{
SQL::insert_news($head, $first, $body, $priority,
$display, $comments, $username);
my $msg = qq(
HEADLINE: $head
FIRST: $first
BODY: $body
COMMENTS: $comments
USERNAME: $username
);
SITE::notify_admin('News Article Added',$msg);
HTML::control_button($sesskey, 'Add News');
}
elsif ($subpage =~ /DeleteNews/)
{
SQL::delete_news($foruser);
HTML::control_button($sesskey, 'Manage News');
}
elsif ($subpage =~ /EditNews/)
{
HTML::manage_news_form($sesskey, $foruser);
HTML::control_button($sesskey, 'Manage News');
}
elsif ($subpage =~ /UpdateNews/)
{
SQL::update_news($newsid, $head, $first, $body, $priority,
$display, $comments);
HTML::control_button($sesskey, 'Manage News');
}
elsif ($subpage =~ /DisplayNews/)
{
my $news_ref = SQL::get_news();
HTML::display_news($news_ref);
}