| 1 | package perl5i::2::Signature; | ||
| 2 | |||
| 3 | use 5.010_000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use perl5i::2::Signature::Method::None; | ||
| 8 | use perl5i::2::Signature::Function::None; | ||
| 9 | |||
| 10 | # A proxy class to hold a method's signature until its actually used. | ||
| 11 | |||
| 12 | use overload | ||
| 13 | q[""] => sub { return $_[0]->as_string }, | ||
| 14 | fallback => 1 | ||
| 15 | ; | ||
| 16 | |||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $class = shift; | ||
| 20 | my %args = @_; | ||
| 21 | |||
| 22 | my $string = $args{signature} // ''; | ||
| 23 | my $is_method = $args{is_method} // 0; | ||
| 24 | |||
| 25 | my $no_params = !$string || $string !~ /\S/; | ||
| 26 | if( $no_params ) { | ||
| 27 | return $is_method ? perl5i::2::Signature::Method::None->new( signature => $string ) | ||
| 28 | : perl5i::2::Signature::Function::None->new( signature => $string ); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | return bless { signature => $string, is_method => $is_method }, $class; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub as_string { | ||
| 36 | my $self = shift; | ||
| 37 | return $self->{signature}; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub make_real { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | require perl5i::2::Signature::Real; | ||
| 44 | bless $self, "perl5i::2::Signature::Real"; | ||
| 45 | |||
| 46 | $self->__parse_signature; | ||
| 47 | } | ||
| 48 | |||
| 49 | # Upgrade to a real signature object | ||
| 50 | # and call the original method. | ||
| 51 | # This should only be called once per object. | ||
| 52 | our $AUTOLOAD; | ||
| 53 | sub AUTOLOAD { | ||
| 54 | my($method) = reverse split /::/, $AUTOLOAD; | ||
| 55 | return if $method eq 'DESTROY'; | ||
| 56 | |||
| 57 | my $self = $_[0]; # leave @_ alone | ||
| 58 | |||
| 59 | # Upgrade to a real object | ||
| 60 | $self->make_real; | ||
| 61 | |||
| 62 | goto $self->can($method); | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; |