CodeKit Perl Handle Creation

How to create a new Perl CodeKit access handle.

new DBIx::CodeKit()

use DBIx::CodeKit;

$codekit = new DBIx::CodeKit($dbh,
                table     => 'ck_code',
                getparam  => \&get_scalar,
                getparams => \&get_list
            ):

The new CodeKit() method returns a new CodeKit handle object.

$dbh is an open DBI connection handle. If you plan to update the code table set it up to autocommit.

The optional 'table' parameter can be used to specify which of several physical code tables to use. The default is 'ck_code'.

Getting Form Variables in Perl

The 'getparam' and 'getparams' function references tell CodeKit where to look for incoming form values for the HTML Select Methods. If this seems complicated you can simply pass in your own values to those methods using the 'value' parameter.

Here is an example showing how CodeKit can get the incoming form values using the popular CGI module:

use DBI;
use CGI;
use DBIx::CodeKit;

# Database connection.  Set these.
my $dbconnect  = "DBI:mysql:webbysoft:localhost";
my $dbusername = "webbysoft";
my $dbpassword = "124c41";

my $dbh = DBI->connect($dbconnect, $dbusername, $dbpassword)
    or die "connecting : $DBI::errstr\n";

my $cgi = new CGI;

my $codekit = new DBIx::CodeKit($dbh,
                   getparam  => sub { $cgi->param(shift) },
                   getparams => sub { $cgi->param((shift).'[]') }
               );

Notice the 'my $cgi' variable holding a reference to the CGI object. The anonymous sub definitions will use that reference when they are called by CodeKit. You can also pass in a reference to a regular named function as illustrated below.

The 'getparam' sub will be called in a scalar context to return single valued HTML form selections. The name of the form variable to return is passed in as the first argument. It should return a scalar value such as a string.

The 'getparams' sub will be called in a list context to return multivalued HTML form selections. The name of the form variable to return is passed in as the first argument *without* the '[]' at the end of the name. Append '[]' to the name if your parameter interface uses that convention.

The 'getparams' sub can return a list, a reference to a list, a scalar, or a reference to a hash. If you return a reference to a hash, the keys are the selections, and the values are true like this:

sub singleparam {
    my $var_name = shift;       # Ignored for simplicity.
    return 'friday';            # Look in POST or GET.
}

sub multiparam {
    my $var_name = shift;       # Ignored for simplicity.
    my $values = {              # Look in POST or GET.
        'new_york'      => 1,
        'san_francisco' => 1,
        'halifax        => 1
    };
    return $values;             # Return a hash ref.
}

my $codekit = new DBIx::CodeKit($dbh,
                   getparam  => \&singleparam,
                   getparams => \&multiparam
               );