[Melbourne-pm] module importing
Mathew Robertson
mathew.robertson at netratings.com.au
Wed May 23 17:40:41 PDT 2007
> Thank you both for your help. Since I'm using Catalyst, the database
> connection bit has to happen at compile time [1], so it looks something like
> this:
>
> package MyModule;
> use strict;
> use warnings;
> our $database;
>
> sub import {
> my ( $class, $name, $value ) = @_;
> $database = "my_prefix_$value";
> print "This gets printed at runtime\n";
> }
>
> __PACKAGE__->connection($database);
>
> print "This gets printed at compile, time, and \$database is undef here\n";
>
> 1;
>
> and over in the test, I'd like to be able to do this:
>
> use MyModule env => 'test';
>
> ...and have it do the connection to the test database. I've tried
> moving the database connection bit into a subroutine, but since it's
> doing all of it's object-creation magic in the background, it needs to
> connect to the database at compile time.
>
I'm not familiar with catalyst, so I'm not sure if this helps but you
could try moving the "connect" inside the import:
package MyModule;
our $database;
our $connection;
sub import {
my ($class,$name,$value) = @_;
$database = $value if $value;
$connection = __PACKAGE->connection($database) if (! $connection &&
$database);
}
sub DESTROY {
$connection->disconnect;
$connection = undef;
}
sub connection {
my ($class, $db) = @_;
...
return $connection;
}
1;
or something like that...
Mathew
More information about the Melbourne-pm
mailing list