| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 3 |  |  | 3 |  | 98703 | use strict; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 101 |  | 
| 2 | 3 |  |  | 3 |  | 17 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 136 |  | 
| 3 |  |  |  |  |  |  | package Method::Assert; | 
| 4 |  |  |  |  |  |  | BEGIN { | 
| 5 | 3 |  |  | 3 |  | 71 | $Method::Assert::VERSION = '0.0.1'; | 
| 6 |  |  |  |  |  |  | } | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | # ABSTRACT: Ensure instance and class methods are called properly | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  | 3 |  | 23 | use Scalar::Util qw(blessed); | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 298 |  | 
| 11 | 3 |  |  | 3 |  | 16 | use Carp qw(confess); | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 1054 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub import { | 
| 14 | 4 |  |  | 4 |  | 424 | my $package = caller(); | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 4 | 50 |  |  |  | 63 | confess("Importing into package 'main' makes no sense") if $package eq 'main'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | my $class_method = sub { | 
| 19 | 25 | 100 |  | 25 |  | 12823 | confess("Method invoked as a function")                       if     @_ == 0; | 
| 20 | 23 | 100 |  |  |  | 181 | confess("Class method invoked as an instance method")         if     blessed( $_[0] ); | 
| 21 | 16 | 50 |  |  |  | 38 | confess("Invocant is a reference, not a simple scalar value") if     ref($_[0]); | 
| 22 | 16 | 50 |  |  |  | 162 | confess("Invocant '$_[0]' is not a subclass of '$package'")   unless $_[0]->isa($package); | 
| 23 | 16 | 100 |  |  |  | 65 | return @_    if wantarray;         # list   context | 
| 24 | 4 | 50 |  |  |  | 13 | return shift if defined wantarray; # scalar context | 
| 25 | 4 |  |  |  |  | 534 | return;                            # void   context | 
| 26 | 4 |  |  |  |  | 17 | }; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | my $instance_method = sub { | 
| 29 | 45 | 100 |  | 45 |  | 10395 | confess("Method invoked as a function")                                           if     @_ == 0; | 
| 30 | 44 | 100 |  |  |  | 194 | confess("Method not invoked as an instance method")                               unless blessed( $_[0] ); | 
| 31 | 41 | 50 |  |  |  | 173 | confess("Invocant of class '" . ref($_[0]) . "' is not a subclass of '$package'") unless $_[0]->isa($package); | 
| 32 | 41 | 50 |  |  |  | 80 | return @_    if wantarray;         # list   context | 
| 33 | 41 | 100 |  |  |  | 189 | return shift if defined wantarray; # scalar context | 
| 34 | 12 |  |  |  |  | 26 | return;                            # void   context | 
| 35 | 4 |  |  |  |  | 16 | }; | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | { | 
| 38 | 3 |  |  | 3 |  | 26 | no strict 'refs'; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 241 |  | 
|  | 4 |  |  |  |  | 7 |  | 
| 39 | 4 |  |  |  |  | 6 | *{ $package . '::class_method' } = $class_method; | 
|  | 4 |  |  |  |  | 26 |  | 
| 40 | 4 |  |  |  |  | 5 | *{ $package . '::instance_method' } = $instance_method; | 
|  | 4 |  |  |  |  | 20 |  | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 4 |  |  |  |  | 258 | return 1; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | 1; | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | __END__ |