File Coverage

blib/lib/syntax.pm
Criterion Covered Total %
statement 47 49 95.9
branch 1 2 50.0
condition n/a
subroutine 13 13 100.0
pod 2 2 100.0
total 63 66 95.4


line stmt bran cond sub pod time code
1 1     1   77180 use strict;
  1         3  
  1         39  
2 1     1   6 use warnings;
  1         3  
  1         75  
3              
4             # ABSTRACT: Activate syntax extensions
5              
6             package syntax;
7             {
8             $syntax::VERSION = '0.004';
9             }
10             BEGIN {
11 1     1   16 $syntax::AUTHORITY = 'cpan:PHAYLON';
12             }
13              
14 1     1   14 use Carp qw( carp );
  1         2  
  1         67  
15 1     1   947 use Data::OptList 0.104 qw( mkopt );
  1         10099  
  1         6  
16              
17 1     1   893 use namespace::clean;
  1         57542  
  1         9  
18              
19             $Carp::Internal{ +__PACKAGE__ }++;
20             $Carp::Internal{ 'Devel::Declare' } ||= 1;
21              
22             sub import_into {
23 2     2 1 5 my ($class, $into, @args) = @_;
24              
25 2         10 my $import = mkopt \@args;
26              
27 2         76 for my $declaration (@$import) {
28 2         6 my ($feature, $options) = @$declaration;
29              
30 2         8 $class->_install_feature(
31             $feature,
32             $into,
33             $options,
34             [@args],
35             );
36             }
37              
38 2         2449 return 1;
39             }
40              
41             sub unimport_from {
42 1     1 1 3 my ($class, $from, @args) = @_;
43              
44 1         2 for my $feature (@args) {
45              
46 1         4 $class->_uninstall_feature(
47             $feature,
48             $from,
49             );
50             }
51              
52 1         88 return 1;
53             }
54              
55             sub import {
56 2     2   22 my ($class, @args) = @_;
57              
58 2         4 my $caller = caller;
59              
60 2         7 return $class->import_into($caller, @args);
61             }
62              
63             sub unimport {
64 1     1   8 my ($class, @args) = @_;
65              
66 1         2 my $caller = caller;
67              
68 1         4 return $class->unimport_from($caller, @args);
69             }
70              
71             sub _parse_feature_name {
72 3     3   5 my ($class, $feature) = @_;
73              
74 3         47 my $name =
75             join '/',
76             map ucfirst,
77             split m{/},
78             join '',
79             map ucfirst,
80             split qr{_}, $feature;
81              
82 3         11 my $file = "Syntax/Feature/${name}.pm";
83 3         4 my $package = $file;
84             s{ \/ }{::}xg, s{ \.pm \Z }{}xgi
85 3         29 for $package;
86              
87 3         10 return $package, $file;
88             }
89              
90             sub _uninstall_feature {
91 1     1   3 my ($class, $feature, $target) = @_;
92              
93 1         3 my ($package, $file) = $class->_parse_feature_name($feature);
94              
95 1         6 require $file;
96 1 50       12 unless ($package->can('uninstall')) {
97 0         0 carp "Syntax extension $package does not know how to uninstall";
98 0         0 return;
99             }
100 1         5 return $package->uninstall(
101             from => $target,
102             identifier => $feature,
103             );
104             }
105              
106             sub _install_feature {
107 2     2   5 my ($class, $feature, $target, $options, $all_params) = @_;
108              
109 2         7 my ($package, $file) = $class->_parse_feature_name($feature);
110              
111 2         2197 require $file;
112 2         495 return $package->install(
113             into => $target,
114             options => $options,
115             identifier => $feature,
116             outer => $all_params,
117             );
118             }
119              
120             1;
121              
122              
123             __END__