Acme/MetaSyntactic.pm

filter: Statementstmt 100% 126 / 126 Branchbran 100% 32 / 32 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 22 / 22 Podpod 100% 7 / 7 totaltotal 100% 190 / 190 CC 22 SCAR 11.8
CC
22
Cov
100%
load_data
6.0
name
6.0
import
5.0
CRAP 3.3
« Acme/MetaSyntactic/List.pm ↑ Summary Acme/MetaSyntactic/Alias.pm »
1 package Acme::MetaSyntactic;
2 51 $Acme::MetaSyntactic::VERSION = '1.014';
3 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 51 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 51 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 51 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use File::Glob;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # some class data
11 51 our $Theme = 'foo'; # default theme
12 51 our %META;
13  
14 # private class method
15 sub _find_themes {
16 52 ▶ my ( $class, @dirs ) = @_;
Subroutine _find_themes: called CC=1 SCAR=0.0
17 return
18 map @$_,
19 1344 grep { $_->[0] !~ /^[A-Z]/ } # remove the non-theme subclasses
20 1344 map { [ ( fileparse( $_, qr/\.pm$/ ) )[0] => $_ ] }
21 52 map { File::Glob::bsd_glob( File::Spec->catfile( $_, qw( Acme MetaSyntactic *.pm ) ) ) } @dirs;
22 }
23  
24 # fetch the list of standard themes
25 51 $META{$_} = 0 for keys %{ { __PACKAGE__->_find_themes(@INC) } };
26  
27 # the functions actually hide an instance
28 51 my $meta = Acme::MetaSyntactic->new( $Theme );
29  
30 # END OF INITIALISATION
31  
32 # support for use Acme::MetaSyntactic 'foo'
33 # that automatically loads the required classes
34 sub import {
35 28 ▶ my $class = shift;
Subroutine import: called CC=5 SCAR=16.1
36  
37 10 my @themes = ( grep { $_ eq ':all' } @_ )
38 28 ▶ ? ( 'foo', grep { !/^(?:foo|:all)$/ } keys %META ) # 'foo' is still first
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq ':all';} @_) ? : 1 27 28
39 : @_;
40  
41 28 ▶ $Theme = $themes[0] if @themes;
Branch: 1 branch
BranchTrueFalseTotal
if @themes 10 18 28
42 28 $meta = Acme::MetaSyntactic->new( $Theme );
43  
44 # export the metaname() function
45 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 28 my $callpkg = caller;
47 28 *{"$callpkg\::metaname"} = \&metaname; # standard theme
48  
49 # load the classes in @themes
50 28 for my $theme( @themes ) {
51 16 eval "require Acme::MetaSyntactic::$theme; import Acme::MetaSyntactic::$theme;";
52 16 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 15 16
53 15 ▶ *{"$callpkg\::meta$theme"} = sub { $meta->name( $theme, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
54 }
55 }
56  
57 sub new {
58 106 ▶ my ( $class, @args ) = ( @_ );
Subroutine new: called CC=3 SCAR=11.0
Pod documented
59 106 my $theme;
60 106 ▶ $theme = shift @args if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 93 13 106
61 106 ▶ $theme = $Theme unless $theme; # same default everywhere
Branch: 1 branch
BranchTrueFalseTotal
unless $theme 13 93 106
62  
63 # defer croaking until name() is actually called
64 106 bless { theme => $theme, args => { @args }, meta => {} }, $class;
65 }
66  
67 # CLASS METHODS
68 sub add_theme {
69 6 ▶ my $class = shift;
Subroutine add_theme: called CC=4 SCAR=13.9
Pod documented
70 6 my %themes = @_;
71  
72 6 for my $theme ( keys %themes ) {
73 6 ▶ croak "The theme $theme already exists!" if exists $META{$theme};
Branch: 1 branch
BranchTrueFalseTotal
if exists $META{$theme} 1 5 6
74 5 my @badnames = grep { !/^[a-z_]\w*$/i } @{$themes{$theme}};
75 5 ▶ croak "Invalid names (@badnames) for theme $theme"
Branch: 1 branch
BranchTrueFalseTotal
if @badnames 1 4 5
76 if @badnames;
77  
78 4 my $code = << "EOC";
79 package Acme::MetaSyntactic::$theme;
80 use strict;
81 use Acme::MetaSyntactic::List;
82 our \@ISA = qw( Acme::MetaSyntactic::List );
83 4 our \@List = qw( @{$themes{$theme}} );
84 1;
85 EOC
86 4 ▶ eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
87 4 $META{$theme} = 1; # loaded
88  
89 # export the metatheme() function
90 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 4 my $callpkg = caller;
92 4 ▶ *{"$callpkg\::meta$theme"} = sub { $meta->name( $theme, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
93 }
94 }
95  
96 # load the content of __DATA__ into a structure
97 # this class method is used by the other Acme::MetaSyntactic classes
98 sub load_data {
99 44 ▶ my ($class, $theme ) = @_;
Subroutine load_data: called CC=6 SCAR=17.9
Pod documented
100 44 my $data = {};
101  
102 44 my $fh;
103 51 ▶ { no strict 'refs'; $fh = *{"$theme\::DATA"}{IO}; }
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 44 my $item;
106 my @items;
107 44 $$item = "";
108  
109 {
110 44 local $_;
111 44 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 350 44 394
112 350 ▶ /^#\s*(\w+.*)$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^#\s*(\w+.*)$/ and do { push @items, $item; $item = $data; my $last; my @keys = split(m[\s+|\s*/\s*], $1, 0); $last = $item, $item = ($item->{$_} ||= {}) foreach (@keys); $item = \($$last{$keys[-1]} = ""); next } 157 193 350
113 157 push @items, $item;
114 157 $item = $data;
115 157 my $last;
116 157 my @keys = split m!\s+|\s*/\s*!, $1;
117 157 ▶ $last = $item, $item = $item->{$_} ||= {} for @keys;
Condition100%2/2
$item->{$_} ||= {}
l!l
92194
MC/DC100%1/1
$item->{$_} ||= {}
$item->{$_}
Truth table100%2/2
A
A= $item->{$_}
Aresult
11
00
118 157 $item = \( $last->{ $keys[-1] } = "" );
119 157 next;
120 };
121 193 s/#.*//; # remove end-of-line comments
122 193 $$item .= $_;
123 }
124 }
125  
126 # avoid leaving all the DATA handles open
127 44 close $fh;
128  
129 # clean up the items
130 44 for( @items, $item ) {
131 201 $$_ =~ s/\A\s*//;
132 201 $$_ =~ s/\s*\z//;
133 201 $$_ =~ s/\s+/ /g;
134 }
135 44 return $data;
136 }
137  
138 # main function
139 5 ▶sub metaname { $meta->name( @_ ) };
Subroutine metaname: called CC=1 SCAR=0.0
Pod documented
140  
141 # corresponding method
142 sub name {
143 40 ▶ my $self = shift;
Subroutine name: called CC=6 SCAR=17.9
Pod documented
144 40 my ( $theme, $count );
145  
146 40 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 28 12 40
147 28 ( $theme, $count ) = @_;
148 28 ▶ ( $theme, $count ) = ( $self->{theme}, $theme )
Branch: 1 branch
BranchTrueFalseTotal
if $theme =~ /^(?:0|[1-9]\d*)$/ 5 23 28
149 if $theme =~ /^(?:0|[1-9]\d*)$/;
150 }
151 else {
152 12 ( $theme, $count ) = ( $self->{theme}, 1 );
153 }
154  
155 40 ▶ if( ! exists $self->{meta}{$theme} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"meta"}{$theme}) 36 4 40
156 36 my ( $Theme, $category ) = split /\//, $theme, 2;
157 36 ▶ if( ! $META{$Theme} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($META{$Theme}) 26 10 36
158 26 eval "require Acme::MetaSyntactic::$Theme;";
159 26 ▶ croak "Metasyntactic list $Theme does not exist!" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 23 26
160 23 $META{$theme} = 1; # loaded
161 }
162 $self->{meta}{$theme}
163 33 = "Acme::MetaSyntactic::$Theme"->new( %{ $self->{args} },
164 ( category => $category )x!! $category );
165 }
166  
167 37 $self->{meta}{$theme}->name( $count );
168 }
169  
170 # other methods
171 9 ▶sub themes { wantarray ? ( sort keys %META ) : scalar keys %META }
Subroutine themes: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 1 9
172 4 ▶sub has_theme { $_[1] ? exists $META{$_[1]} : 0 }
Subroutine has_theme: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 3 1 4
173  
174 51 1;
175  
176 __END__
« Acme/MetaSyntactic/List.pm ↑ Summary Acme/MetaSyntactic/Alias.pm »