BabelKit Perl Handle Creation

[Perl API] [Index] [Code Descriptions] ## BabelKit Perl Handle Creation How to create a new Perl BabelKit access handle. #### new DBIx::BabelKit()
    use DBIx::BabelKit;

    $babelkit = new DBIx::BabelKit($dbh,
                    table     => 'bk_code',
                    getparam  => \&get_scalar,
                    getparams => \&get_list
                ):
The new BabelKit() method returns a new BabelKit 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 'bk_code'. #### Getting Form Variables in Perl The 'getparam' and 'getparams' function references tell BabelKit 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 BabelKit can get the incoming form values using the popular CGI module:
    use DBI;
    use CGI;
    use DBIx::BabelKit;

    # 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 $babelkit = new DBIx::BabelKit($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 BabelKit. 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 $babelkit = new DBIx::BabelKit($dbh,
                       getparam  => \&singleparam,
                       getparams => \&multiparam
                   );
[Perl API] [Index] [Code Descriptions]