http://qs321.pair.com?node_id=553814


in reply to Creating packages on the fly

I would think that a UNIVERSAL::AUTOLOAD method would be the only way to catch this, if you want to avoid any predefinitions...

Another approach maybe to find all Wx:: classes and subclass them at startup... This may subclass a whole bunch of classes that you won't be using, but it avoids AUTOLOAD nastiness... Something like the following maybe:

use Wx; my $symbol_table = Symbol::Table->New(PACKAGE=>'main::Wx'); while(my $package = each %$symbol_table) { eval "package Groninger::Wx::$package; use base 'Wx::$package;"; }

This code doesn't recurse (so only packages directly in the Wx:: namespace will be subclassed) and it only works if the packages in that namespace have already been use'd. But that could be done automatically I suppose...

But perhaps it would then make more sense to generate them specifically for the packages you want to subclass using a syntax like:

use Class::SubclassSet Groninger => qw(Wx::App Wx::Frame Wx::Menu); package Class::SubclassSet; sub import { my ($this,$into,@classes); for my $class (@classes) { eval "package ${into}::$class; use base '$class';"; } }

That would reduce the duplicate code you'd be typing, prevent redundant subclassing and avoid UNIVERSAL::AUTOLOAD;

(All code untested; just brainstorming here...)

Regards, Giel