title: How can I check if a database query will return results?
body: <p>Our website uses Perl to provide a simple mechanism for our HR people to post vacancies to our website. It was developed by a third party, but they have been long since kicked into touch, and sadly we do not have any Perl skills in-house. This is what happens when Marketing people circumvent their in-house IT team!</p>
  
  <p>I need to make a simple change to this application. Currently, the vacancies page says 'We currently have the following vacancies:', regardless of whether there are any vacancies! So we want to change it so that this line is only displayed at the appropriate times.</p>
  
  <p>I could, obviously, start to learn a bit of Perl, but we are already planning a replacement site, and it certainly won't be using Perl. So since the solution will be  trivial for those with these skills, I thought I'd ask for some focused help.</p>
  
  <p>Below is the start of the procedure that lists the vacancies. </p>
  
  <pre><code>sub list {
    require HTTP::Date;
    import HTTP::Date;
  
    my $date = [split /\s+/, HTTP::Date::time2iso(time())]-&gt;[0];
  
    my $dbh = DBI-&gt;connect($dsn, $user, $password)
      || die "cannot connect to $database: $!\n";
  
    my $sql = &lt;&lt;EOSQL;
  SELECT * FROM $table where expiry &gt;= '$date' order by expiry
  EOSQL
  
    my $sth = $dbh-&gt;prepare($sql);
    $sth-&gt;execute();
  
  
    while (my $ref = $sth-&gt;fetchrow_hashref()) {
      my $temp  = $template;
      $temp     =~ s#__TITLE__#$ref-&gt;{'title'}#;
  
      my $job_spec = $ref-&gt;{'job_spec'};
  
  ...etc...
  </code></pre>
  
  <p>The key line is <code>while (my $ref = $sth-&gt;fetchrow_hashref()) {</code>. I'm figuring that this is saying 'while I can pull off another vacancy from the returned recordset...'. If I place my print statement before this line, it will always be shown; after this line and it was be repeated for every vacancy.</p>
  
  <p>How do I determine that there are some vacancies to be displayed, without prematurely moving through the returned recordset?</p>
  
  <p>I could always copy the code within the while loop, and place it within an if() statement (preceding the while loop) which will also include my print statement. But I'd prefer to just have the simpler approach of <code>If any records then print "We currently have.." line</code>. Unfortunately, I haven't a clue to code even this simple line.</p>
  
  <p>See, I told you it was a trivial problem, even considering my fumbled explanation!</p>
  
  <p>TIA</p>
  
  <p>Chris</p>