Commits

Shlomi Fish committed 156ebfb

Change to a dispatch hash.

  • Participants
  • Parent commits cdc9a64

Comments (0)

Files changed (1)

         $result = $SET_VAR__SUCCESS;
     }
 
-    if ($result == $SET_VAR__SUCCESS) {
-        $self->_event_say("Variable '$variable' in module '$modulename' has changed.");
-    } elsif ($result == $SET_VAR__CANNOT_HANDLE_TYPE) {
-        $self->_event_say("Variable '$variable' is of type ".ref($module->{$variable}).' and I do not know how to set that kind of variable!');
-    # we don't know that variable!
-    } elsif ($result == $SET_VAR__UNKNOWN_VAR) { 
-        if ($module->{$variable}) { # well we do, but only to read
-            $self->_event_say("Variable '$variable' in module '$modulename' is read-only, sorry.");
-        } else { # not known
-            $self->_event_say("Module '$modulename' does not have a variable '$variable' as far as I can tell.");
-        }
-    } elsif ($result == $SET_VAR__WRONG_FORMAT_FOR_ARRAY) {
-        $self->_event_say("Variable '$variable' is a list. To add to a list, please use the '+' symbol before the value (vars <module> <variable> '+<value>'). To remove from a list, use the '-' symbol (vars <module> <variable> '-<value>').");
-    } elsif ($result == $SET_VAR__WRONG_FORMAT_FOR_HASH) {
-        $self->_event_say("Variable '$variable' is a hash. To add to a hash, please use the '+' symbol before the '|key|value' pair (vars <module> <variable> '+|<key>|<value>').  The separator symbol ('|' in this example) could be anything. To remove from a list, use the '-' symbol (vars <module> <variable> '-<key>').");
-    } elsif ($result == $SET_VAR__ALREADY_REPORTED_SUCCESS) {
-        # already reported success
-    } elsif ($result == $SET_VAR__USED_LETTER_TO_DELIMIT_SECTIONS) {
-        $self->_event_say("Variable '$variable' in module '$modulename' has changed, but may not be what you expect since it appears to me that you used a letter to delimit the sections. I hope that is what you meant to do...");
-    } elsif ($result > 0) { # negative = success
+    my %dispatch = 
+    (
+        $SET_VAR__SUCCESS => sub {
+            $self->_event_say("Variable '$variable' in module '$modulename' has changed.");
+        },
+        $SET_VAR__CANNOT_HANDLE_TYPE => sub {
+            $self->_event_say("Variable '$variable' is of type ".ref($module->{$variable}).' and I do not know how to set that kind of variable!');
+        },
+        $SET_VAR__UNKNOWN_VAR => sub {
+            if ($module->{$variable}) { # well we do, but only to read
+                $self->_event_say("Variable '$variable' in module '$modulename' is read-only, sorry.");
+            } else { # not known
+                $self->_event_say("Module '$modulename' does not have a variable '$variable' as far as I can tell.");
+            }
+        },
+        $SET_VAR__WRONG_FORMAT_FOR_ARRAY => sub {
+            $self->_event_say("Variable '$variable' is a list. To add to a list, please use the '+' symbol before the value (vars <module> <variable> '+<value>'). To remove from a list, use the '-' symbol (vars <module> <variable> '-<value>').");
+        },
+        $SET_VAR__WRONG_FORMAT_FOR_HASH => sub {
+            $self->_event_say("Variable '$variable' is a hash. To add to a hash, please use the '+' symbol before the '|key|value' pair (vars <module> <variable> '+|<key>|<value>').  The separator symbol ('|' in this example) could be anything. To remove from a list, use the '-' symbol (vars <module> <variable> '-<key>').");
+        },
+        $SET_VAR__USED_LETTER_TO_DELIMIT_SECTIONS => sub {
+            $self->_event_say("Variable '$variable' in module '$modulename' has changed, but may not be what you expect since it appears to me that you used a letter to delimit the sections. I hope that is what you meant to do...");
+        },
+    );
+
+    if (my $callback = $dispatch{$result}) {
+        $callback->();
+    }
+    elsif ($result > 0) { # negative = success
         $self->_event_say("Variable '$variable' in module '$modulename' could not be set for some reason unknown to me.");
     }