[Melbourne-pm] module importing

Josh Heumann melbourne.pm at joshheumann.com
Wed May 23 00:30:51 PDT 2007


<quote who='leif.eriksen at hpa.com.au' when='Wed, May 23, 2007 at 04:10:57PM +1000'>
> Without looking at any reference material, a dim memory says you need to
> write a MyModule::import() sub, and look at the arg list therein....


<quote who='Mathew Robertson' when='Wed, May 23, 2007 at 04:46:18PM +1000'>
> So.... basically create a sub called "import" - the first argument is
> the package/class name.  The rest of the arguments are whatever you
> supply as arguments to the 'use'.  Parse the args and store your value
> in an 'our' variable.

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.

It might not be possible to do what I want this way, which wouldn't be
the end of the world.

J

[1]: AFAIK.  If someone knows more about how Catalyst works, or if
there's even a way to tell it which db to use that's internal to
Catalyst, please let me know.


More information about the Melbourne-pm mailing list