Mark Knoop <> wrote:

: I'm looking at something like the following:
: 
: use strict;
: use warnings;
: use DBI;
: use DBD::ODBC; # can this live solely in the module?

    You don't need this at all. DBI calls the appropriate driver
from the connection string.


: use Common::CSAdmin; # this is my module
: 
: my $dbh = GetDBHandle('DSN');

    You are expecting GetDBHandle() to be imported into this
namespace. You'll need to export it from Common::CSAdmin. Without
a specific export you'll need to tell perl what namespace to look
in.

my $dbh = Common::CSAdmin::GetDBHandle('DSN'); 

[snip]
: package Common::CSAdmin;
: 
: use strict;
: use warnings;
: 
: use DBI;
: use DBD::ODBC;

    Not needed. DBI calls this automatically.

: sub GetDBHandle($)  {}

    Also not needed unless you intend to run code in here which
finds it helpful.

: sub GetDBHandle($) {
:       my $dsn = shift(@_);
:       my ($dbusr,$dbpwd,$server,$dbh);
:       $dbusr = 'usr';
:       $dbpwd = 'pwd';
:       $server = 'server';
:       $dbh = DBI-> connect("DBI:ODBC:$dsn", $dbusr, $dbpwd, {RaiseError =>
: 1, PrintError => 1})  || die "Could not connect to datasource $dsn";
:       return $dbh;
: }

    IIRC, RaiseError warns on errors and PrintError dies on errors.
So your error message would never be seen. A DBI expert might shed
more light on this, but you probably want this to avoid duplicate
warn and die messages.

        $dbh = DBI->connect(
                "DBI:ODBC:$dsn",
                $dbusr,
                $dbpwd,
                {
                        RaiseError => 0,
                        PrintError => 1,
                }
        );

: 
: 1;


    If all you are ever going to do is return $dbh.


my $dbh = Common::CSAdmin::GetDBHandle('DSN'); 


package Common::CSAdmin;
    
use strict;
use warnings;

sub GetDBHandle($) {
        my $dsn = shift(@_);
        my ($dbusr,$dbpwd,$server,$dbh);
        $dbusr = 'usr';
        $dbpwd = 'pwd';
        $server = 'server';
        $dbh = DBI-> connect(
                "DBI:ODBC:$dsn",
                $dbusr,
                $dbpwd,
                {
                        RaiseError => 0,
                        PrintError => 1,
                }
        );
        return $dbh;
}

1;


    If you want to export functions into the callers namespace,
check out the Exporter module. It is often considered bad form to
automatically export functions. It is also easier to debug later
if you specifically call functions while using the module. The
strftime() function is a good example.

use POSIX qw(strftime);

    When debugging a script it sure helps to know where a
mysterious subroutine came from.


        An OO module for such a simple thing is probably going
overboard. It makes for a nice programming exercise, but a
function is not an object.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to