in reply to Passing a database connection

You retyped, you didn't cut'n'paste. You're actually calling it as Main->new( $dbh ), not Main::new( $dbh ). Using Main->new() calls it as a method and passes the receiver (in this case, 'Main') in as the first parameter.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: Passing a database connection
by aufflick (Deacon) on Nov 23, 2005 at 00:30 UTC
    If the rest of your code uses OO mechanisms, you might want to recode new() so it will work as a class method rather than calling it as a non OO sub (ie. Main::new). The example below also checks that the passed in var is a valid DBI object. If you had done that in your code, you would have received an error as soon as you called the new method.
    package Main; use strict; my $dbh; sub new { my $class = shift; my $dbh = shift; UNIVERSAL::isa($dbh, 'DBI') or die "Main->new() was invoked without a valid DBI object"; }
Re^2: Passing a database connection
by Spidy (Chaplain) on Nov 22, 2005 at 21:36 UTC