blib/lib/Const/Common/Generator.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 95.6% 44 / 46 CC 5 SCAR 10.4
CC
5
Cov
95%
generate
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.8
↑ Summary
1 package Const::Common::Generator;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.01";
7  
8 2 ▶use Text::MicroTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub generate {
Subroutine generate: called CC=5 SCAR=16.1
Pod documented
11 1 my $class = shift;
12 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
13  
14 1 my $package = $args{package};
15 1 my @constants = @{ $args{constants} };
16  
17 1 my @consts;
18 1 ▶ while (my ($name, $value) = splice @constants, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@constants, 0, 2)) 3 1 4
19 3 my $comment;
20 3 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 1 2 3
21 1 $comment = $value->{comment};
22 1 $value = $value->{value};
23 }
24  
25 3 ▶ push @consts, {
Branch: 1 branch
BranchTrueFalseTotal
defined $comment ? : 1 2 3
26 name => $name,
27 value => $value,
28 (defined($comment) ? (comment => $comment) : ()),
29 };
30 }
31  
32 my $gen = Text::MicroTemplate->new(
33 template => $class->_template,
34 6 ▶ escape_func => sub {shift},
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 )->build;
36  
37 1 $gen->(
38 package => $package,
39 consts => \@consts
40 )->as_string;
41 }
42  
43 1 ▶sub _template {
Subroutine _template: called CC=1 SCAR=0.0
44 1 <<'...';
45 ? my %args = @_;
46 ? use Scalar::Util qw/looks_like_number/;
47 package <?= $args{package} ?>;
48 use strict;
49 use warnings;
50 use utf8;
51  
52 use Const::Common (
53 ? for my $const (@{ $args{consts} }) {
54 ? my $v = $const->{value};
55 <?= $const->{name} ?> => <?= looks_like_number($v) ? '' : "'" ?><?= $v ?><?= looks_like_number($v) ? '' : "'" ?>,<? if ($const->{comment}) { ?> # <?= $const->{comment} ?><? } ?>
56 ? }
57 );
58  
59 1;
60 ...
61 }
62  
63 2 1;
64 __END__
↑ Summary