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;

In reply to Re: Creating packages on the fly by Gilimanjaro
in thread Creating packages on the fly by Jouke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.