McDarren has asked for the wisdom of the Perl Monks concerning the following question:
I have a piece of code that looks like this:
Basically, it just sets a flag (or not) if any one of multiple conditions are met. It seems to work fine as I have it, but I now find that I need to extend it so that a message is presented to the user, indicating which condition was met. I'm trying to avoid a series of if/elsif's, so my initial thought was to do something like:$send_flag = $bbstatus ne $last_bbstatus ? 1 : ($now - $last_send_time) > $min_frequency ? 1 : defined($OPTS::opts{f}) + ? 1 : + 0;
And then....$send_flag = $bbstatus ne $last_bbstatus ? 1 : ($now - $last_send_time) > $min_frequency ? 2 : defined($OPTS::opts{f}) + ? 3 : + 10;
my %send_messages = ( 1 => "Status has changed", 2 => "Last send too old", 3 => "Forced send requested", 10 => "No changes" );
However, the above seems a bit unsightly, and I'm wondering if this is a situation where a dispatch table would be appropriate? And if so, how would this be structured? (I don't have any experience with dispatch tables, and although I've read a few related threads here and here, I'm afraid I'm having trouble grasping exactly when they are appropriate and how they are constructed).
Appreciate any advice.
Thanks,
Darren :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is a Dispatch Table appropriate here?
by Belgarion (Chaplain) on Dec 14, 2005 at 05:38 UTC | |
|
Re: Is a Dispatch Table appropriate here?
by NetWallah (Canon) on Dec 14, 2005 at 06:32 UTC | |
|
Re: Is a Dispatch Table appropriate here?
by Roy Johnson (Monsignor) on Dec 14, 2005 at 15:32 UTC | |
|
Re: Is a Dispatch Table appropriate here?
by planetscape (Chancellor) on Dec 14, 2005 at 14:27 UTC | |
|
Re: Is a Dispatch Table appropriate here?
by GrandFather (Saint) on Dec 14, 2005 at 08:30 UTC |