Developer

Convert an ASP application to Perl

It's possible to take an application that's written in ASP and convert it to Perl. Here's how.

This article originally appeared as a Web Development Zone e-newsletter.

By Phillip Perkins

Some Web Development Zone e-newsletter subscribers have requested examples of applications created in a language other than ASP. I do the majority of my Web development in ASP; however, you can use other scripting languages to create Web solutions. In this tip, I'll give you an example application written in ASP and convert it to Perl.

Most Web applications are just another solution for data entry, manipulation, and reporting, and the foundation for all data solutions is the connection to the persisted data. The code below shows a data solution written in ASP:

. . .
Dim rs, conn, strConn
strConn = "some connection string"
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = strConn
conn.Open
Set rs = Server.CreateObject("ADODB.Recordset")
Set rs.ActiveConnection = conn
rs.Open "SELECT * FROM USERS"
%>
<HTML>
<BODY>

<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="1">
<THEAD>
    <TH>User ID</TH>
    <TH>First Name</TH>
    <TH>Last Name</TH>
</THEAD>
<%
Do While Not rs.EOF
    Response.Write "<TR>"
    Response.Write "<TD>" & rs.Fields("USER_ID") & "</TD>"
    Response.Write "<TD>" & rs.Fields("FIRST_NAME") & "</TD>"
    Response.Write "<TD>" & rs.Fields("LAST_NAME") & "</TD>"
    Response.Write "</TR>"
    rs.MoveNext
Loop
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing

%>
</TABLE>

</BODY>
</HTML>
. . .

This code creates a connection to a database, runs a query to return a Recordset, loops through the Recordset to get the "USER_ID", "FIRST_NAME", and "LAST_NAME" fields, and then writes it out to the Response object. It then kills its connection and cleans up.

Now if we want to do the same thing in Perl, only connecting to a MySQL database, we need to know what similarities and differences exist between the two languages. In ASP, when we connect to data, we generally use ActiveX Data Objects (ADO).

However, when we connect to data in Perl, we use the DataBase Interface (DBI) package. Both of these wrap up functionality into nice little packages so you can use them in an object-oriented fashion.

The method for connecting to a database in Perl is very similar to that in ASP:

use DBI;
my $dsn = "dbi:mysqlPP:database=test;host=localhost;";
my $dbh = DBI->connect($dsn, "username", "password");
my $sth = $dbh->prepare("SELECT * FROM USERS");
$sth->execute;

print <<EOF;
<HTML>
<BODY>

<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="1">
<THEAD>
    <TH>User ID</TH>
    <TH>First Name</TH>
    <TH>Last Name</TH>
</THEAD>
EOF
while (($user_id, $first_name, $last_name) = $sth->fetchrow_array) {
    print "<TR>";
    print "<TD>" . $user_id . "</TD>";
    print "<TD>" . $first_name . "</TD>";
    print "<TD>" . $last_name . "</TD>";
    print "</TR>";
}
$sth->finish;
$dbh->disconnect;
print <<EOF;
</TABLE>

</BODY>
</HTML>
EOF
exit;

When using the DBI package, you must first inform the interpreter/compiler that you'll be using that package. Just like connecting to a database in ADO, you create a connection to the database that you intend to use. However, ADO uses the Connection object, whereas DBI returns a database handle ($dbh). Also, you'll see that the connect methods are a little different. Instead of the connection string you use in ADO, you use a data source name ($dsn).

When you want to return a result set from the database, ADO returns a Recordset object. This object contains the methods and properties necessary to manipulate and navigate the result set. DBI returns a statement handle ($sth) that exposes functionality to navigate the result set.

The difference between the Recordset object and the statement handle is the methods used to navigate the result set. The Recordset object provides methods for moving throughout the result set in any direction, and its data is exposed through the Fields collection.

The statement handle provides methods for fetching data from the current record much like a fetch on a database cursor. However, in this example, we return an array of data that constitutes the fields of the current record. We store this data to local variables to use later—when we print out the data.

Finally, when we're finished, we simply close our statement handle with finish, similar to the way we execute the Close method on the Recordset object. We also disconnect the database handle in the same manner as we Close the Connection object. Our print buffer is piped through the open port through standard output upon exit.

Phillip Perkins is a contractor with Ajilon Consulting. His experience ranges from machine control and client/server to corporate intranet applications.

Editor's Picks

Free Newsletters, In your Inbox