title: Perl argument switch based on regex
body: <p>I have a Perl script that takes in arguments.  When I had single value arguments, the following code sufficed:</p>
  
  <pre><code>                    switch ($ARGV[0]) {
  
                      case "--cmd1" {
                              $action = "cmd1";
                      }
  
  
                      case "--cmd2" {
                              $action = "cmd2";
                      }
  </code></pre>
  
  <p>Now, I have a case where the command, <code>cmd3</code> has a parameter, as in <code>--cmd3=SOMETHING</code>.  Since <code>SOMETHING</code> can vary, the simple switch/case does not work anymore.  Basically, I need to do a switch/case on the command itself.  I thought I could use a regex with the first matching group being the command and the second being the optional equals.  The following does not work, but it illustrates what I'm trying to do.</p>
  
  <pre><code>                    $ARGV[0] =~ m/(.*?)(=.*){0,1}/;
                      my $cmd = $1;
                      my $equals = $2;
  
                      switch ($cmd) {
  
                      case "--cmd1" {
                              $action = "cmd1";
                      }
  
                      case "--cmd2" {
                              $action = "cmd2";
                      }
  
                      case "--cmd3" {
                              $action = "cmd3";
                              print $equals;
                      }
  </code></pre>
  
  <p>:::::::::::::::::::::::::::::::::::EDIT:::::::::::::::::::::::::::::::::::::::::::::::::::::</p>
  
  <p>I figured it out, but I'll give the guy who answered upvotes and accept anyway.  I could not use if because that means restructuring everything.  Here's the solution.</p>
  
  <pre><code>                   switch ($ARGV[0]) {
  
                      case "--cmd1" {
                              $action = "cmd1";
                      }
  
                      case "--cmd2" {
                              $action = "cmd2";
                      }
  
                      case m/--cmd3(=.*)?/ {
                              $ARGV[0] =~ m/--cmd3(=.*)?/;
                              $action = "cmd3";
                              print $1;
                      }
  </code></pre>