Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Creating packages on the fly

by Gilimanjaro (Hermit)
on Jun 06, 2006 at 13:38 UTC ( #553814=note: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re^2: Creating packages on the fly
by Jouke (Curate) on Jun 06, 2006 at 18:38 UTC
    Hey Giel,
    Thanks for this. If I can't achieve what I want using the UNIVERSAL::AUTOLOADER idea (although it looks like it), I'm going to try this one.

    Jouke Visser
    Using Perl to enable the disabled: pVoice

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://553814]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2022-08-09 19:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found