in reply to Qualified package variable access

When I do this sort of thing, I return the dbh back to the caller, as follows:

package ECClib; my $dbh; sub initialise(){ . . $dbh = db_connect($dbuser, $dbpasswd, $dbserver); . . return $dbh )
and
use ECClib; my $dbh = ECClib::initialise(); my $sql="select InputID from ECCInput order by InputID"; my $sth = $dbh->prepare($sql);

Replies are listed 'Best First'.
Re^2: Qualified package variable access
by tobyink (Canon) on Dec 17, 2020 at 22:03 UTC

    Better yet:

    package ECClib; my $dbh; sub get_dbh { $dbh ||= db_connect( $dbuser, $dbpasswd, $dbserver ); }

    And:

    use ECClib; { my $dbh = ECClib->get_dbh; ...; } { my $dbh = ECClib->get_dbh; # gets the same instance! ...; }

    If you're using Perl 5.10+:

    package ECClib; use v5.10; sub get_dbh { state $dbh = db_connect( $dbuser, $dbpasswd, $dbserver ); }