File Coverage

blib/lib/Method/Signatures/Utils.pm
Criterion Covered Total %
statement 35 39 89.7
branch 6 10 60.0
condition 5 6 83.3
subroutine 8 8 100.0
pod 0 4 0.0
total 54 67 80.6


line stmt bran cond sub pod time code
1             package Method::Signatures::Utils;
2              
3 63     63   219 use strict;
  63         64  
  63         1422  
4 63     63   187 use warnings;
  63         69  
  63         1147  
5 63     63   185 use Carp;
  63         66  
  63         2782  
6              
7 63     63   221 use base qw(Exporter);
  63         63  
  63         24429  
8             our @EXPORT = qw(new_ppi_doc sig_parsing_error carp_location_for DEBUG);
9              
10             sub DEBUG {
11 1476 50   1476 0 9263 return unless $Method::Signatures::DEBUG;
12              
13 0         0 require Data::Dumper;
14 0         0 local $Data::Dumper::Sortkeys = 1;
15 0 0       0 print STDERR "DEBUG: ", map { ref $_ ? Data::Dumper::Dumper($_) : $_ } @_;
  0         0  
16             }
17              
18              
19             sub new_ppi_doc {
20 327     327 0 291 my $code = shift;
21              
22 327         25916 require PPI;
23 327 50       4930655 my $ppi = PPI::Document->new($code) or
24             sig_parsing_error(
25             "source '$$code' cannot be parsed by PPI: " . PPI::Document->errstr
26             );
27              
28 327         298347 return $ppi;
29             };
30              
31              
32             # Generate cleaner error messages...
33             sub carp_location_for {
34 85     85 0 151 my ($class, $target) = @_;
35 85 100       258 $target = qr{(?!)} if !$target;
36              
37             # using @CARP_NOT here even though we're not using Carp
38             # who knows? maybe someday Carp will be capable of doing what we want
39             # until then, we're rolling our own, but @CARP_NOT is still serving roughly the same purpose
40 85         76 our @CARP_NOT;
41 85         168 local @CARP_NOT;
42 85         135 push @CARP_NOT, 'Method::Signatures';
43 85 100       71 push @CARP_NOT, $class unless $class =~ /^${\__PACKAGE__}(::|$)/;
  85         643  
44 85         148 push @CARP_NOT, qw< Class::MOP Moose Mouse Devel::Declare >;
45              
46             # Skip any package in the @CARP_NOT list or their sub packages.
47 85         170 my $carp_not_list_re = join '|', @CARP_NOT;
48 85         940 my $skip = qr/^ $carp_not_list_re (?: :: | $ ) /x;
49              
50 85         96 my $level = 0;
51 85         73 my ($pack, $file, $line, $method);
52 85   100     74 do {
      66        
53 327         7678 ($pack, $file, $line, $method) = caller(++$level);
54             } while $method !~ $target and $method =~ /$skip/ or $pack =~ /$skip/;
55              
56 85         2867 return ($file, $line, $method);
57             }
58              
59             sub sig_parsing_error {
60 17     17 0 28 my ($file, $line) = carp_location_for(__PACKAGE__, 'Devel::Declare::linestr_callback');
61 17         51 my $msg = join('', @_, " in declaration at $file line $line.\n");
62 17         279 die($msg);
63             }
64              
65             1;