txpsj has asked that I explain how this works,
so here goes.
All packages implicitly inherit from the UNIVERSAL package.
This is like every package's @ISA always ending in
'UNIVERSAL' no matter what you set a package's @ISA to be.
So if you do Data::Dumper->new(), then Perl
looks for "sub Data::Dumper::new". If you've
already done use Data::Dumper or
require Data::Dumper, then Perl finds it and
things progress as normal.
But, if Perl doesn't find it, it looks in
@Data::Dumper::ISA for packages that Data::Dumper
inherits from in case the new() method is supposed to be
inherited. But no Data::Dumper::new() means no
@Data::Dumper::ISA yet so it is empty. But Perl still
implicitly includes the UNIVERSAL package in the list and
so looks for "sub UNIVERSAL::new". Well, we've given Perl
one to find.
So Data::Dumper->new( "x", "y" ) becomes
UNIVERSAL::new( "Data::Dumper", "x", "y" ).
So we pull the package name off the argument list and
do a require on it. This should define a
real "sub Data::Dumper::new" which we then call.
All this brings to my mind some improvements I should make.
First, it would be nice to "goto" the real new() so that
the call stack doesn't show the extra UNIVERSAL::new() in
case new() wants to look at caller(), for
example. Also, if require Data::Dumper didn't
define "sub Data::Dumper::new", then we'd be in a infinite
loop.
So here is a better version (updated):
package UNIVERSAL;
use strict;
sub new {
my( $pkg )= @_;
eval "require $pkg; 1"
or die "$@";
my $meth= $pkg->can( "new" );
die "Package $pkg defines no new() method"
if $meth == \&UNIVERSAL::new;
goto &$meth;
}
1;
Now put this in AutoNew.pm and change the two lines you
always start all of your scripts with to:
#!/usr/bin/perl -w -mAutoNew
use strict;
Now you can create whatever objects you want without
predeclaring what packages you need. This, of course,
makes you code harder to maintain and I'd strongly advise
against it for production code. But it was fun.
-
tye
(but my friends call me "Tye") |