package WTF; use strict; use warnings; use Carp; sub define { my $self = shift; my $type = shift || croak "No type name"; my ($code, $parent, $subclass) = ('', __PACKAGE__, __PACKAGE__ . "\::$type"); # Get the package code template { local $/ = undef; while () { s,##PACKAGE##,$subclass,g; s,##PARENT##,$parent,g; $code .= $_; } my $WARNING = ''; local $SIG{__WARN__} = sub { $WARNING = "@_" }; eval $code; croak "Failed to create subclass: $subclass - $WARNING$@" if $WARNING || $@; croak "Problem with subclass: $subclass - cannot new()" unless $subclass->can(qw/new/); } print STDERR "Created type: $type\n"; } sub import { my $self = shift; while (@_) { my ($nm, $list) = splice(@_, 0 ,2); __PACKAGE__->define($nm => $list); } } 1; __DATA__ package ##PACKAGE##; use strict; use warnings; use ##PARENT##; my @ISA = ( qw/##PARENT##/ ); sub new { my $self = shift; bless \( my $scalar ), ref $self || $self; } #### use strict; use warnings; # Emulate use BEGIN { require WTF; WTF->import(qw/some/); WTF->import(qw/more/); } #### C:\WINDOWS\system32\cmd.exe /c perl WTF.pl Created type: some Problem with subclass: WTF::more - cannot new() at WTF.pl line 8 BEGIN failed--compilation aborted at WTF.pl line 9, line 1. shell returned 255 Hit any key to close this window... #### !perl WTF.pl Created type: some Problem with subclass: WTF::more - cannot new() at WTF.pl line 8 BEGIN failed--compilation aborted at WTF.pl line 9, line 1.