in reply to Trying to re-use code at different levels of an inherited object.

Right after the server went down I realized that you can do this with closures, but you can't use SUPER to do it.
#FILE Foo.pm package Foo; use strict; __PACKAGE__->make_do_something(); sub make_do_something { my $class = shift; my $meth_name = "do_something"; my $parent = $class->can($meth_name); no strict 'refs'; *{"$class\::$meth_name"} = sub { my $self = shift; if ($parent) { print "$class is not the root\n"; $self->$parent(@_); } print "Foo $class!!!\n"; return 1; }; } 1; __END__ #FILE Bar.pm package Bar; use base 'Foo'; use strict; __PACKAGE__->make_do_something( ); sub new { bless {}, shift(); } 1; __END__ #FILE foobar.pl #!/usr/local/bin/perl #File: foobar.pl use Foo; use Bar; $b = new Bar(); $b->do_something(); __END__
  • Comment on Re (tilly) 1 (closure): Trying to re-use code at different levels of an inherited object.
  • Download Code