title: perl foreach loop issue on breaking out
body: <p>I am writing a small perl script, primarily to learn the language. Basically it has an action dispatch table. Based on the user input any one of the target actions will be invoked. Each action is a small, independent utility function (say print time), working on which allows me to explore and learn different aspects of perl.</p>
  
  <p>I have run into a problem with the dispatch mechanism. The script runs in a continuous loop, each time taking a user request for an action. This input is compared against the regular expressions of each available action. If there is a match, that action is executed and it breaks out of the match loop to read user's next request. The problem I am facing is that, if I request for the same action twice, it is not matching the second time. If I print the dispatch table immediately after the match, the matched action entry seems to be missing. If I continuously request for same action, it works only on alternate invocations. If I avoid the "last LABEL", it works without any issues. </p>
  
  <p>Perl version is 5.12.4 (on Fedora 15, 32 bit). Below is a simplified but complete example. I am still a beginner in perl. Please excuse if it doesn't meet the standards of a monk :) Kindly help in figuring out the issue with this code. Your help is much appreciated.</p>
  
  <pre><code>#!/bin/env perl  
  
  use strict ;  
  use warnings ;  
  use Text::ParseWords ;  
  
  my @Actions ;  
  my $Quit ;  
  
  sub main  
  {  
      # Dispatch table  
      # Each row has [syntax, help, {RegExp1 =&gt; Function1, RegExp2 =&gt; Function2,...}]  
      # There can be multiple RegExps depending on optional arguments in user input   
      @Actions =  
      (     
          ['first &lt;value&gt;',  'Print first',       {'first (.*)'  =&gt; \&amp;PrintFirst}  ],  
          ['second &lt;value&gt;', 'Print second',      {'second (.*)' =&gt; \&amp;PrintSecond} ],  
          ['quit',           'Exits the script',  {'quit'        =&gt; \&amp;Quit}        ]  
      ) ;  
  
      my $CommandLine ;  
      my @Captures ;  
      my $RegEx ;  
      my $Function ;  
  
      while(!$Quit)  
      {  
          # Get user input, repeat until there is some entry  
          while(!$CommandLine)  
          {  
              print "\$ " ;  
              my $argline = &lt;STDIN&gt; ;  
              my @args = shellwords($argline) ;  
              $CommandLine = join (" ", grep { length() } @args) ;  
          }  
  
          # Find and execute the action matching given input  
          # For each entry in the @Actions array  
          ExecAction: foreach my $Action (@Actions)  
          {  
              # For each RegExp for that action (only 1 per action in this example)  
              while (($RegEx, $Function) = each %{@$Action[2]})  
              {  
                  if (@Captures = $CommandLine =~ m/^$RegEx$/i)  
                  {  
                      print "Match : $RegEx\n" ;  
                      &amp;$Function(@Captures) ;  
                      last ExecAction ; # Works if this line is commented  
                  }  
              }  
          }  
  
          $CommandLine = undef ;    
      }  
  }  
  
  sub PrintFirst  { print "first $_[0]\n" ; }  
  sub PrintSecond { print "second $_[0]\n" ; }  
  sub Quit        { $Quit = 1 ; }  
  
  main ;  
  </code></pre>