Does this weird piece of code do what you want?
(Needless note to others: Don't use this code. Note to self: Don't write such code!)
package StupidAutoloader;
use strict;
use warnings;
use Carp;
our %Classes;
sub import {
my $class = shift;
my @hierarchies = @_;
%Classes = map {($_, undef)} @hierarchies;
*UNIVERSAL::new = sub {
my $proto = $_[0];
my $class = ref($proto) || $proto;
croak "Could not find method new() in package '$class'."
if ref($proto) or
exists $INC{$class} or
not in_hierarchy(\%Classes, $class);
eval "use $class;";
croak "Error loading module '$class': $@" if $@;
no strict 'refs';
goto &{"${class}::new"};
};
}
sub in_hierarchy {
my $h = shift;
my $c = shift;
return 1 if exists $h->{$c};
my @levels = split /::/, $c;
my $accum;
foreach (@levels) {
$accum .= $_;
return 1 if exists $h->{"${accum}::*"}
}
return 0;
}
1;
---
#!/usr/bin/perl
use strict;
use warnings;
use StupidAutoloader 'Data::*';
my $d = Data::Dumper->new([]);
print "$d\n";
my $g = Data::Grouper->new(a => []);
print "$g\n";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|