Home > Articles > Web Development > Perl

  • Print
  • + Share This

Binding Result Columns

For convenience, result values can be bound to variables in a manner similar to binding placeholder values. DBI provides the bind_columns method, which enables predefined variables to be associated through reference to values returned from a statement handle. By making the connection between returned values and variables only once for a given statement, bind_columns gives the convenience of using custom variables with the performance of using references.

The bind_columns method is best used in combination with returned references (using fetch or fetchrow_arrayref as mentioned in the previous section, "Retrieving Data Sets as References") to provide the best performance for large data sets. In Listing 4, line 11 binds the result columns to the $childid, $title, $username, and $created variables. The variables are file-scoped through the my keyword in line 02, and then passed to bind_columns as references in the order in which their values are returned from the query prepared in line 03. With each call to $sth->fetch in line 13, the variables passed in line 11 are assigned the values of the next result row.

Bound results can be useful for substituting data for variables placed in template text. If a program such as Listing 4 were used to fill values in a provided template with Perl-style variables embedded, the text could be evaluated by using an eval statement within the while loop, as follows:

my $template = q{<li><a href="tree.psp?msgid=$childid">$title</a>};
while ($sth->fetch)
{
 eval "print $template";
}

This type of automated variable substitution provides improved performance in a situation where it's most needed. Templates are usually used in situations in which processing them shouldn't incur overhead over a custom-coded application. Therefore, everything that can be done to limit processing time should be done. Luckily, the abstraction afforded by a template provides the opportunity to tune statements such as these without affecting the way the application behaves.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.