in reply to Re^2: __PACKAGE__ in anonysub
in thread __PACKAGE__ in anonysub

I know I'm not grasping all of your requirements here ... so maybe this will help you along your way.

package Drone; sub new { return bless {}, shift; } 1; package Factory; no strict 'refs'; sub fixup { #*Drone::bar = sub { return __PACKAGE__ }; my $class = shift || __PACKAGE__; *Drone::bar = sub { return $class }; } 1; package Subfactory; use base qw( Factory ); 1; package main; Subfactory->fixup; my $foo = Drone->new; print $foo->bar . "\n";
This prints out "Subfactory".

Replies are listed 'Best First'.
Re^4: __PACKAGE__ in anonysub
by thpfft (Chaplain) on Feb 19, 2005 at 20:52 UTC

    Thank you! That won't work in my real situation, because I won't be calling a class method like that, but you made me realise that instead of __PACKAGE__, I just need to call ref $self, which will inherit properly. D'oh:

    my $self = shift; my $factory_class=ref $self; *Foo::bar = sub { return $factory_class->instance; };

    I definitely am creating a closure now, but I don't think it will matter: it's only a simple scalar that gets unnaturally prolonged.

    Thanks.