[Chicago-talk] import as initialize?

Jim Thomason jim at jimandkoka.com
Tue Mar 14 09:32:04 PST 2006

Is there a community consensus on how import should be used?

Personally, I really like the concept of using the thing as a class
initialize method as such things exist in other languages. I've even
been known to do it a few times. It's just so convenient to use it as
a class initialization method.

But, alas, I also know that it doesn't work very well to do that due
to the simple fact that you can use a module with empty parens or
require it and completely skip over that step of calling ->import
automagically. Voila, your class is unitialized and things break

So is there some other handy dandy way to do class initialization
other than using this more-fragile-than-I'd like approach of import?

Sure, I can always go into every single class and call an initialize
method in it as my last step or first step or what have you, but that
seems kludgy.

In case it helps, the specific example I'm thinking of is something I
did up in Basset::Object:

our %imported = ();

sub import {
	my $class = shift;
	my $conf = $class->conf;
	my $myconfig = $conf->{$class};

	return if $imported{$class}++;

	foreach my $method (keys %$myconfig) {
		if ($class->can($method)) {


For each subclass loaded, it looks at the conf file and initializes
all class methods to the values defined in the conf file. But the
second somebody requires a module, it chokes. Most unpleasant.

My options as I see them are:
1) require the users to bring in modules with use and no parens. This
is the current approach that I'm not too keen on.
2) Explicitly call initialize (or something like that) in every single
subclass. I want to avoid this.
3) Some magical third solution that you (yes you!) the reader can suggest.

So what do you guys think?


More information about the Chicago-talk mailing list