title: Passing psql query from perl into string
body: <p>Currently I have a perl script that accesses our database, performs certain queries and prints output to the terminal. Instead, I would like to output the results into a template latex file before generating a pdf. For most of my queries I pull out numbers and store these as scalar variables (eg how often a particular operator carries out a given task). eg.</p>
  
  <pre><code>foreach $op (@operator) {
      $query = "SELECT count(task_name) FROM table WHERE date &lt;= '$date_stop' and 
                date &gt;= '$date_start' and task=\'$operator[$index]\';";
  
      #execute query
      $result=$conn-&gt;exec($query);
      $conres = $conn-&gt;errorMessage;
      if ($result-&gt;resultStatus eq PGRES_TUPLES_OK) {
          if($result-&gt;ntuples &gt; 0) {
              ($task[$index]) = $result-&gt;fetchrow;
          }
          printf("$operator[$index] carried out task: %d\n", $task[$index]);   
      } else {
          die "Failed.\n$conres\n\n";
          exit -1;
      }
      $index++;
  } 
  printf("**********************************\n\n");
  </code></pre>
  
  <p>In the final report I will summarise how many times each operator completed each task in a table. In addition to this there will also be some incidents which must be reported. I can print these easily to the terminal using a command such as </p>
  
  <pre><code>$query = "SELECT operator, incident_type from table_name WHERE incident_type = 'Y' 
            and date &lt;= '$date_stop' and date &gt;= '$date_start';";
  $result=$conn-&gt;exec($query);
  $conres = $conn-&gt;errorMessage;
  if ($result-&gt;resultStatus eq PGRES_TUPLES_OK) {
      if($result-&gt;ntuples &gt; 0) {
          $result-&gt;print(STDOUT, 1, 1, 0, 0, 0, 1, "\t", "", "");
      }
  } else {
      die "Failed.\n$conres\n\n";
      exit -1;
  }
  </code></pre>
  
  <p>An example of the output of this command is</p>
  
  <pre><code>operator  |   incident_type
  -----------------------------
  AB        |  Incomplete due to staff shortages
  -------------------------------
  CD        |  Closed due to weather
  -----------------------------
  </code></pre>
  
  <p>How can I make my perl script pass the operator names and incidents into a string array rather than just sending the results to the terminal?</p>