| 1 | # reform::implicit.pm | ||
| 2 | # | ||
| 3 | # Provides "self", "base" and "class" accessors to using classes. | ||
| 4 | # Written by Henning Koch <jaz@netalive.org>. | ||
| 5 | # Two methods stolen from Simon Cozens' rubyisms.pm. | ||
| 6 | |||
| 7 | package reform::implicit; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use base 'Exporter'; | ||
| 11 | our @EXPORT = ('self', 'base', 'class'); | ||
| 12 | |||
| 13 | # Accessor to base class methods. | ||
| 14 | sub base | ||
| 15 | { | ||
| 16 | # $class is the package in which the method lies | ||
| 17 | # that just called base(). This might be the grandparent | ||
| 18 | # or grand-grandparent of self's class, so we may not | ||
| 19 | # deduce it from self. | ||
| 20 | my $class = (caller(0))[0]; | ||
| 21 | reform::implicit::BaseCaller->new(self(1), $class); | ||
| 22 | } | ||
| 23 | |||
| 24 | # Accessor to the current package. | ||
| 25 | sub class | ||
| 26 | { | ||
| 27 | my $class = self(1); | ||
| 28 | ref $class and $class = ref $class; | ||
| 29 | $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | # Accessor to the current instance. | ||
| 33 | # Stolen from Simon Cozens' rubyisms.pm. | ||
| 34 | sub self | ||
| 35 | { | ||
| 36 | my($uplevel) = @_; | ||
| 37 | |||
| 38 | my $call_pack = (caller($uplevel))[0]; | ||
| 39 | |||
| 40 | # So we're looking for the first thing that ISA $call_pack | ||
| 41 | my $level = 1; | ||
| 42 | while (caller($level)) | ||
| 43 | { | ||
| 44 | my @their_args = DB::uplevel_args($level); | ||
| 45 | if (ref $their_args[0] | ||
| 46 | and eval { $their_args[0]->isa($call_pack) }) | ||
| 47 | { | ||
| 48 | return $their_args[0]; | ||
| 49 | } | ||
| 50 | $level++; | ||
| 51 | } | ||
| 52 | # Well, hey, maybe it's a class method. | ||
| 53 | return $call_pack; | ||
| 54 | } | ||
| 55 | |||
| 56 | # Gets the arguments of a subroutine call some frames up. | ||
| 57 | # Stolen from Simon Cozens' rubyisms.pm. | ||
| 58 | package DB; | ||
| 59 | sub uplevel_args { my @foo = caller($_[0]+1); return @DB::args }; | ||
| 60 | |||
| 61 | |||
| 62 | # Object on which the base accessor works. | ||
| 63 | package reform::implicit::BaseCaller; | ||
| 64 | |||
| 65 | # Pre-reform style constructor. | ||
| 66 | sub new | ||
| 67 | { | ||
| 68 | my($class, $object, $calling_class) = @_; | ||
| 69 | # $class is the package in which the method lies | ||
| 70 | # that just called base(). | ||
| 71 | # We may NOT get $class from $object if we ever want to | ||
| 72 | # use base more than one level of inheritance up. | ||
| 73 | my $self = { object => $object, | ||
| 74 | class => $calling_class }; | ||
| 75 | bless($self, $class); | ||
| 76 | $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Since the BaseCaller has no other methods itself, all | ||
| 80 | # method calls to the base calls should land here. | ||
| 81 | sub AUTOLOAD | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | our ($AUTOLOAD); | ||
| 85 | |||
| 86 | my $method = $AUTOLOAD; | ||
| 87 | $method =~ /(\w+)$/; | ||
| 88 | $method = $1; | ||
| 89 | |||
| 90 | return if $method eq 'DESTROY'; | ||
| 91 | |||
| 92 | my $object = $self->{object}; | ||
| 93 | my $class = $self->{class}; # may be parent of $object (see above) | ||
| 94 | |||
| 95 | # print "OBJECT: $object\n"; | ||
| 96 | # print "CALLING: $class\n"; | ||
| 97 | # print "\$object->$class\:\:SUPER\:\:$method(\@_)\n"; | ||
| 98 | |||
| 99 | my @re = eval "\$object->$class\:\:SUPER\:\:$method(\@_)"; | ||
| 100 | $@ and die "Error calling base method: $@"; | ||
| 101 | return @re; | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | 1; |