note
gellyfish
<p>
I'm not entirely sure that what I am about to suggest is very sensible as it may well break all sorts of other things, so anyone of a sensitive nature should look away now.
</p>
<p>It is entirely possible to add an <code>AUTOLOAD</code> subroutine to the <code>UNIVERSAL</code> package which every other package inherits from implicity, so you can do something like:
<code>
#!/usr.bin/perl
use strict;
use warnings;
Foo::Bar->baz();
Foo::Bar->baz();
package UNIVERSAL;
sub AUTOLOAD
{
my ($class, @args) = @_;
our $AUTOLOAD;
(my $method = $AUTOLOAD) =~ s/.*:://;
print "$AUTOLOAD was called via autoload\n";
no strict 'refs';
*{$AUTOLOAD} = sub { print "I'm alive\n"};
$class->$method;
}
</code>
This creates your methods in unknown packages on the fly. If you want to do subclassing on the fly you can do something like:
<code>
#!/usr.bin/perl
use strict;
use warnings;
Foo::Bar->baz();
Foo::Bar->baz();
package UNIVERSAL;
sub AUTOLOAD
{
my ($class, @args) = @_;
our $AUTOLOAD;
(my $method = $AUTOLOAD) =~ s/.*:://;
(my $parent = $class) =~ s/::[^:]*$//;
print "$AUTOLOAD was called via autoload\n";
no strict 'refs';
push @{"${class}::ISA"},$parent;
$class->$method;
}
package Foo;
sub baz
{
my ($self) = @_;
print "I'm alive\n";
}
</code>
</P>
<p>But don't say I didn't warn you that this was probably a bad idea</p>
<p>/J\</p>
553761
553761