| 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
| |||||||||||||
| 39 | : @_; | ||||||||||||
| 40 | |||||||||||||
| 41 | 28 | ▶ | $Theme = $themes[0] if @themes; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 61 | 106 | ▶ | $theme = $Theme unless $theme; # same default everywhere | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 74 | 5 | my @badnames = grep { !/^[a-z_]\w*$/i } @{$themes{$theme}}; | |||||||||||
| 75 | 5 | ▶ | croak "Invalid names (@badnames) for theme $theme" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 112 | 350 | ▶ | /^#\s*(\w+.*)$/ && do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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->{$_} ||= {}
MC/DC100%1/1
$item->{$_} ||= {}
$item->{$_}
Truth table100%2/2
A
A= $item->{$_}
| |||||||||||||
| 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
| |||||||||||||
| 147 | 28 | ( $theme, $count ) = @_; | |||||||||||
| 148 | 28 | ▶ | ( $theme, $count ) = ( $self->{theme}, $theme ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 156 | 36 | my ( $Theme, $category ) = split /\//, $theme, 2; | |||||||||||
| 157 | 36 | ▶ | if( ! $META{$Theme} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | 26 | eval "require Acme::MetaSyntactic::$Theme;"; | |||||||||||
| 159 | 26 | ▶ | croak "Metasyntactic list $Theme does not exist!" if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 172 | 4 | ▶ | sub has_theme { $_[1] ? exists $META{$_[1]} : 0 } | ||||||||||
|
Subroutine
has_theme: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 173 | |||||||||||||
| 174 | 51 | 1; | |||||||||||
| 175 | |||||||||||||
| 176 | __END__ | ||||||||||||