Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Creating packages on the fly

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


in reply to Creating packages on the fly

Here's what I whipped up using UNIVERSAL::AUTOLOAD; it seems to actually work! It also attempts to store an already setup UNIVERSAL::AUTOLOAD and use it if the requested package isn't one that should be auto-generated, so it should be safe to use with other UNIVERSAL::AUTOLOAD'ing stuff, as long as this one is use'd last (or the others are smart too).

#!/usr/bin/perl use strict; use warnings; use lib '.'; use Wx; use Class::Generate::Auto 'Jouke::Wx' => 'Wx'; my $w = Jouke::Wx::App->new; exit; package Class::Generate::Auto; use strict; use warnings; our $AUTOLOAD; our %mapping; my $old_ua = undef; sub import { my $this = shift; while(@_) { my $prefix = shift; my $superclass = shift; $mapping{$prefix} = $superclass; } my $current_ua = UNIVERSAL->can('AUTOLOAD'); if($current_ua and $current_ua!=\&autoloader) { $old_ua = $current_ua; } *UNIVERSAL::AUTOLOAD=\&autoloader; } sub autoloader { my ($package,$method) = $AUTOLOAD =~ /^(.*)::(.*)$/; return if $method eq 'DESTROY'; keys %mapping; while(my ($prefix,$superclass) = each %mapping) { my ($extra) = $package =~ /^${prefix}::(.*)$/ or next; $superclass .= "::$extra" if $extra; eval "package $package; use base q{$superclass};"; die "Unable to auto-generate class $package as subclass from $ +superclass: $@" if $@; my $method_ref = $package->can($method); die "Freshly auto-generated class $package can't $method (prob +ably $superclass can't either)" unless $method_ref; goto $method_ref; } goto $old_ua if $old_ua; die "Class $package can't $method and no autoloader was present be +fore ".__PACKAGE__." was loaded"; } 1;


Comment on Re: Creating packages on the fly
Download Code
Replies are listed 'Best First'.
Re^2: Creating packages on the fly
by Jouke (Curate) on Jun 07, 2006 at 06:37 UTC
    hehehe...very nice....and scary close to what I have here. I'm running into some practical problems however, that I'm trying to work out now.
    The problems I run into now are related to the specific implementation I had in mind. I'm sure that with your and gellyfish's suggestions I'll be able to work it out tho'

    Thanks guys!


    Jouke Visser
    Using Perl to enable the disabled: pVoice

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2015-07-31 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (277 votes), past polls