• # assign fields to variables
    my ($id,$first,$middle,$last);
    $sth->bind_columns(undef, \$id, \$first, \$middle, \$last);

    In preparation for reading in the data from MySQL, you bind the data (in column form) to variables using the bind_columns command.

    In other words, you are matching up the variables to the data you're requesting from MySQL Server.

  • # output president's names listing
    print "The presidents in order:\n";
    while($sth->fetch()) {
    print "$first ";
    print "$middle " if ($middle);
    print "$last\n";
    }

    In this portion of the Perl program, you translate the data from the returned statement handle into your variables, and then print immediately to the standard output - the screen.

    The fetch command fills up your variables with data from the database, as the while programming loop moves through the rows (records) in the database.

    Some of the presidents in your list don't have a middle name, so you add an if statement (if ($middle)) to tell the program not to stop if a president doesn't have one.

    The \n character creates a new line, acting as a carriage return while printing to the screen.

  • # clean up
    $sth->finish();

    # disconnect from database
    $dbh->disconnect;

    Finally, you finish the statement handle, and disconnect the database handle. This ends the connection between the Perl program and the MySQL Server database.