| 1 | package Devel::Spy::_obj; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | ## WARNING!!!! HEY!! Read this! | ||
| 6 | |||
| 7 | # This package should be as spotless as possible. Don't define or | ||
| 8 | # import any functions here because then they'll shadow that if it's | ||
| 9 | # also defined in the objects that are being wrapped. | ||
| 10 | |||
| 11 | # Seriously. Make recursion fatal. I hit this alot when writing this | ||
| 12 | # kind of code and it helps to have a backstop. | ||
| 13 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use overload (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use Sub::Name (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 0 | ▶ | use UNIVERSAL::ref; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use Devel::Spy::Util (); | ||
| 19 | use Devel::Spy::_constants; | ||
| 20 | use Devel::Spy::_overload; | ||
| 21 | |||
| 22 | # Called by UNIVERSAL::ref. | ||
| 23 | # | ||
| 24 | # TODO: what if my called object also would like ->ref invoked as a method? | ||
| 25 | sub ref { | ||
| 26 | return CORE::ref( $_[Devel::Spy::SELF][Devel::Spy::UNTIED_PAYLOAD] ); | ||
| 27 | } | ||
| 28 | |||
| 29 | # Do all the proxy work for methods (other than isa and can) here. | ||
| 30 | # | ||
| 31 | # TODO: what if my wrapped object needs an AUTOLOAD too? | ||
| 32 | use vars '$AUTOLOAD'; | ||
| 33 | |||
| 34 | sub AUTOLOAD { | ||
| 35 | my $method = $AUTOLOAD; | ||
| 36 | $method =~ s/^Devel::Spy::_obj:://; | ||
| 37 | |||
| 38 | my $self = shift @_; | ||
| 39 | my $class = Scalar::Util::blessed( $self->[Devel::Spy::UNTIED_PAYLOAD] ); | ||
| 40 | |||
| 41 | # Redispatch and log, maintaining context. | ||
| 42 | if (wantarray) { | ||
| 43 | |||
| 44 | # Log before. | ||
| 45 | my $followup = $self->[Devel::Spy::CODE]->( " \@->$method(" | ||
| 46 | . join( ',', map overload::StrVal($_), @_ ) | ||
| 47 | . ')' ); | ||
| 48 | |||
| 49 | # Redispatch. | ||
| 50 | my @results = $self->[Devel::Spy::UNTIED_PAYLOAD]->$method(@_); | ||
| 51 | |||
| 52 | # Log after. | ||
| 53 | $followup = $followup->( | ||
| 54 | ' ->(' . join( ',', map overload::StrVal($_), @results ) . ')' ); | ||
| 55 | |||
| 56 | return @results; | ||
| 57 | } | ||
| 58 | elsif ( defined wantarray ) { | ||
| 59 | |||
| 60 | # Log before. | ||
| 61 | my $followup = $self->[Devel::Spy::CODE]->( " \$->$method(" | ||
| 62 | . join( ',', map overload::StrVal($_), @_ ) | ||
| 63 | . ')' ); | ||
| 64 | |||
| 65 | # Redispatch. | ||
| 66 | my $result = $self->[Devel::Spy::UNTIED_PAYLOAD]->$method(@_); | ||
| 67 | |||
| 68 | # Log after. | ||
| 69 | $followup = $followup->( ' ->' . overload::StrVal($result) ); | ||
| 70 | |||
| 71 | return Devel::Spy->new( $result, $followup ); | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | |||
| 75 | # Log before. | ||
| 76 | my $followup = $self->[Devel::Spy::CODE]->( " V->$method(" | ||
| 77 | . join( ',', map overload::StrVal($_), @_ ) | ||
| 78 | . ')' ); | ||
| 79 | |||
| 80 | # Redispatch. | ||
| 81 | $self->[Devel::Spy::UNTIED_PAYLOAD]->$method(@_); | ||
| 82 | |||
| 83 | # Log after? | ||
| 84 | |||
| 85 | return; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # TODO: what if my called object should accept this DESTROY call? | ||
| 90 | sub DESTROY { } | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | __END__ | ||