File Coverage

blib/lib/Acme/What.pm
Criterion Covered Total %
statement 59 59 100.0
branch 9 10 90.0
condition 2 2 100.0
subroutine 17 17 100.0
pod n/a
total 87 88 98.8


line stmt bran cond sub pod time code
1             package Acme::What;
2              
3 1     1   26757 use 5.010;
  1         5  
  1         41  
4 1     1   6 use strict;
  1         2  
  1         49  
5              
6             BEGIN {
7 1     1   3 $Acme::What::AUTHORITY = 'cpan:TOBYINK';
8 1         34 $Acme::What::VERSION = '0.002';
9             }
10              
11 1     1   6 use Carp qw/croak/;
  1         1  
  1         72  
12 1     1   2162 use Devel::Declare;
  1         48778  
  1         7  
13 1     1   1144 use Sub::Name qw/subname/;
  1         748  
  1         69  
14              
15 1     1   7 use parent qw/Devel::Declare::Context::Simple/;
  1         2  
  1         7  
16              
17             sub import
18             {
19 1     1   28603 no strict 'refs';
  1         2  
  1         492  
20            
21 3     3   4415 my $caller = caller;
22 3         53 my $self = shift;
23 3   100     17 my $method = shift // 'WHAT';
24            
25 3         7 my $export = "$caller\::what";
26 3 100       11 unless ( exists &$export )
27             {
28 1 50       11 $self = $self->new unless ref $self;
29             Devel::Declare->setup_for(
30             $caller,
31 5     5   220 { what => { const => sub { $self->_parser(@_) } } }
32 1         18 );
33 1     5   37 *$export = subname $export => sub ($) { $self->_do(@_) };
  5     5   2620  
34             }
35            
36 3 100       96 $^H{(__PACKAGE__)} = $method =~ m{^\+(.+)$}
37             ? $1
38             : sprintf("$caller\::$method");
39             }
40              
41             sub unimport
42             {
43 1     1   70 $^H{(__PACKAGE__)} = undef;
44             }
45              
46             sub _parser
47             {
48 5     5   8 my $self = shift;
49 5         22 $self->init(@_);
50 5         65 $self->skip_declarator;
51 5         134 $self->skipspace;
52 5         45 my $linestr = $self->get_linestr;
53            
54 5         33 my $remaining = substr($linestr, $self->offset);
55            
56 5 100       35 if ($remaining =~ /^(.*?);(.*)$/)
57             {
58             # Found semicolon
59 2         6 my $quoted = $self->_quote($1);
60 2         9 substr($linestr, $self->offset) = "('$quoted');" . $2;
61             }
62             else
63             {
64 3         5 chomp $remaining;
65 3         7 my $quoted = $self->_quote($remaining);
66 3         11 substr($linestr, $self->offset) = "('$quoted');\n";
67             }
68            
69 5         34 $self->set_linestr($linestr);
70             }
71              
72             sub _quote
73             {
74 5     5   10 my ($self, $str) = @_;
75 5         8 $str =~ s{([\\\'])}{\\$1}g;
76 5         10 return $str;
77             }
78              
79             sub _do
80             {
81 1     1   5 no strict 'refs';
  1         2  
  1         117  
82 5     5   15 my ($self, @args) = @_;
83 5         19 my @caller = caller(1);
84            
85 5         144 my $meth = $caller[10]{ (__PACKAGE__) };
86            
87 5 100       38 croak "Acme::What disabled"
88             unless defined $meth;
89            
90 4         24 return $meth->(@args);
91             }
92              
93             __PACKAGE__
94             __END__