Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

Comment on Re: Creating packages on the fly
Select or Download Code
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
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 making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-07-13 10:08 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (249 votes), past polls