title: How can I iterate through all checked values of a checkbox group in a Perl CGI script?
body: <p>I have an <a href="http://www.apachefriends.org/en/xampp.html" rel="nofollow">xampp</a> test system setup running on a Windows machine.</p>
  
  <p>I have a small command line application I would like to trigger using a 
  Perl CGI script.</p>
  
  <p>I have a form that I created using Perl. There are a number of check boxes as well as an input box where a user can type a message.</p>
  
  <p>Here is the code that I have so far. I am not an expert in perl by any means, so there 
  might be a few things in here that don't make sense, or are for testing purposes</p>
  
  <pre><code>#!"C:\xampp\perl\bin\perl.exe"
  
  #print a standard 200 -level HTTP header
  print "Content-Type:text/html\n\n";
  
  print "&lt;html&gt;&lt;head&gt;&lt;title&gt;Broadcast Message&lt;/title&gt;&lt;/head&gt;\n";
  print "&lt;body&gt;\n";
  
  #display form data
  #&amp;displayInfo();
  
  print "&lt;/body&gt;&lt;/html&gt;\n";
  
  if ($ENV{REQUEST_METHOD} eq "GET") 
    {
      &amp;formDisplay();
      exit;
    }
  #Else process and display back to screen
  else
    {
      #&amp;switches();
      &amp;parseform();
      &amp;displayInfo();
  
      exit;
    }  
  
  #This subroutine will display information without using the HTML document
  
  sub formDisplay
    {
      print qq~
  
      &lt;FORM METHOD="POST" ACTION="/cgi-bin/broadcast.cgi"&gt;
        &lt;h3&gt;Broadcast Message to Domain&lt;/h3&gt;
        ***PLEASE NOTE*** This will broadcast a message to ALL machines in the Domain&lt;/br&gt;&lt;/br&gt;
        &lt;TABLE cellpadding=0 cellspacing=1 border=0&gt;
        &lt;TR&gt;
          &lt;TD&gt;Select your target by switch:&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp   &lt;/br&gt;&lt;/TD&gt;
          &lt;TD&gt;&lt;/TD&gt;
          &lt;TD&gt;Select your target by department:                       &lt;/br&gt;&lt;/TD&gt;
        &lt;/TR&gt;
        &lt;TR&gt;
          &lt;TD&gt;&lt;input type=CHECKBOX name=517 value="517"&gt;   - 517      &lt;/br&gt;&lt;/TD&gt;
  
          &lt;TD&gt;&lt;/TD&gt;
          &lt;TD&gt;&lt;input type=CHECKBOX name=ACC value="ACC"&gt;   - ACC      &lt;/br&gt;&lt;/TD&gt;
        &lt;/TR&gt;
        &lt;TR&gt;
          &lt;TD&gt;&lt;input type=CHECKBOX name=testfiletxt value="testfiletxt"&gt; - testefiletxt &lt;/br&gt;&lt;/TD&gt;
        &lt;/TR&gt;
        &lt;/TABLE&gt;
        &lt;/br&gt;&lt;/br&gt;
        Please type the message you wish to broadcast to the Domain: &lt;/br&gt;
        &lt;input name="message"&gt;&lt;/br&gt;
  
        &lt;input type=SUBMIT name=Send value="Send"&gt;
        &lt;input type=RESET name=Reset value="Reset"&gt;
      &lt;/form&gt;
      ~;
    }
  
  #This subroutine will display information received from a form 
  sub displayInfo {
    print "&lt;b&gt;Your Message:&lt;/b&gt; &lt;/br&gt;  ",         $form{"message"}, "&lt;/br&gt;";
    $system_message=$form{"message"};
    #system "sent.exe /u:testmachine \"$system_message\"" ;
    print qq~
    &lt;form&gt;
    &lt;input type="button" value="Back" onclick="history.go(-1);return true;"&gt;
    &lt;/form&gt;
    ~;
    # Working loop for form hash
    delete $form{'Send'};
  
    foreach(keys %form) {
    print "$form{$_} &lt;/br&gt;";
    }
   ##############################
    $textme=$form{"testfiletxt"}; 
      open( FILE, "&lt; $textme" ) or die "Can't open $filename : $!";
  
      while( &lt;FILE&gt; ) {
          chomp;
          print "sent.exe $_ ", $form{"message"} , "&lt;/BR&gt;";
      }
  
      close FILE; 
  
  }
  
  
  sub parseform
    {
      #get data from environment variable
      read STDIN,$qstring,$ENV{"CONTENT_LENGTH"};
      #$qstring = $ENV{'QUERY_STRING'};
  
      #break data up on ampersands, and store in array
      @pairs = split(/&amp;/, $qstring);
  
      #start a loop to process form data
      foreach (@pairs) {
      #split field name and value on "=", store in two scalar variables
      ($key, $value) = split(/=/);
      #translate '+' signs back to spaces
      $value =~ tr/+/ /;
      #translate special characters
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      #store data in hash
      $form{$key} = $value;
      }
    }
  </code></pre>
  
  <p>What I am having issues with is processing the text boxes that have been checked.</p>
  
  <p>What I need the script to do is to run the command line utility with the parameters specified by the user (i.e. the check boxes, and input box).</p>
  
  <p>How I am thinking this will work is as follows:</p>
  
  <ol>
  <li><p>We have pre built text files that will contain <a href="http://blogs.msdn.com/b/larryosterman/archive/2007/07/11/how-do-i-compare-two-different-netbios-names.aspx" rel="nofollow">Netbios names</a> of computers in our network, one computer name per line. These files will represent switches, each switch file will contain the Netbios name of the computers attached to it. We will eventually be expanding this into doing groups as well.</p></li>
  <li><p>The user will go to this broadcast page and select the switches they wish to broadcast to. So, any machine connected to a particular switch will receive this broadcasted message. The user will also provide a message to be broadcast.</p></li>
  <li><p>When the user clicks "Submit" There are a few things that need to happen.</p>
  
  <ul>
  <li>The file representing the selected switch will need to be opened and the message that the user entered needs to be appended to the end of each Netbios name in the list. Example:</li>
  </ul>
  
  <p>netbiosName message provided by user</p>
  
  <ul>
  <li><p>Then, once that is completed. The command line utility needs to be run using the files that we just edited in the following format:</p>
  
  <pre><code> sent.exe /t:15 /f:$filename
  </code></pre></li>
  </ul>
  
  <p>So if the user selected 3 switches the command should be run 3 times</p>
  
  <pre><code>     sent.exe /t:15 /f:swtich1
       sent.exe /t:15 /f:switch2
       sent.exe /t:15 /f:switch3
  </code></pre></li>
  </ol>
  
  <p>I should point out that the <code>/f</code>" option tells the <code>sent.exe</code> command to use a file that is required to be formatted in a specific way.</p>
  
  <p>I have had some success with processing one option at a time.</p>
  
  <p>But I am stuck with what will happen when the user selects more then one check box.</p>
  
  <p>How can I process the selected check boxes easily using a loop?</p>
  
  <p>Can I pull this from the <code>&amp;parseform</code> sub-routine? Or is there an easier way of doing it?</p>