CGI Perl : while loop in CGI perl

Hi Team,

I am trying to connect to database(succeeded ) and print the records on the browser using while loop. But the elements of array are not displayed instead while loop is displayed directly. Instead of the below I can embed html statements in print but I am looking for the below style as I need to include javascripts and jqueries too.

Could anyone please let me know how to modify the below script(in html tags only) to print the contents of the array.

#!C:\Dwimperl\perl\bin\perl.exe

# Display script errors.
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use DBI qw(:sql_types);
# Display script errors.
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;   
$q=new CGI;

 
 ############  database connectiivty ####################
my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, $th, @newRow);
$databaseName = "dbi:mysql:tcs";
$databaseUser = "root";
$databasePw = "root123";

# Connect to the database
# Note this connection can be used to 
# execute more than one statement
# on any number of tables in the database

$dbh = DBI->connect($databaseName, $databaseUser, $databasePw) || die "Connect failed: $DBI::errstr\n";
 
$i=0;
 
$row=();
$sql = "select p_hostname,p_startdate,p_stime from bt_report where p_recovered='-'";

$query2 = "select count(*) from bt_report where p_recovered='-'" ;

$sth = $dbh->prepare($sql)  or die "Cannot prepare: " . $dbh->errstr(  );

$sth2 = $dbh->prepare($query2);
$sth2->execute();
$sth->execute(  ) or die "Cannot execute: " . $sth->errstr(  );

$rowcount= $sth2->fetchrow_array();
 
@final_data=();
while (@row = $sth->fetchrow_array(  )) {
$output= join(",", @row);
push(@final_data,$output);
}

$sth->finish(  );

# Begin the page.
   print "Content-type: text/html\n\n";
   print qq~<!doctype html>
<head>
<title>Network Availability Report</title>
while ( $i < $rowcount )
{
   print $final_data[$i];

}


</body>
</html>
~;

Check out this info:CGI Perl script is printing the HTML (along with 'content-type: text/html') string to the browser rather than rendering it. How to fix? - Stack Overflow