lib/AI/MicroStructure/List.pm

filter: Statementstmt 80.0% 64 / 80 Branchbran 50.0% 6 / 12 Conditioncond 54.5% 6 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 82.3% 14 / 17 Podpod 100% 4 / 4 totaltotal 74.0% 97 / 131 CC 11 SCAR 13.6
CC
11
Cov
74%
name
9.1
init
3.3
structure
3.2
CRAP 3.9
« lib/AI/MicroStructure/ObjectParser.pm ↑ Summary lib/AI/MicroStructure/WordBlacklist.pm »
1 package AI::MicroStructure::List;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use AI::MicroStructure (); # do not export metaname and friends
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AI::MicroStructure::RemoteList;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @ISA = qw( AI::MicroStructure::RemoteList );
9 1 our $VERSION = '1.001';
10  
11 sub init {
12 2 ▶ my ($self, $data) = @_;
Subroutine init: called CC=3 SCAR=11.9
Pod documented
13 2 my $class = caller(0);
14  
15 2 ▶ $data ||= AI::MicroStructure->load_data($class);
Condition33%1/3
$data ||= "AI::MicroStructure"->load_data($class)
l!l&&r!l&&!r
020
MC/DC0%0/2
$data ||= "AI::MicroStructure"->load_data($class)
$data "AI::MicroStructure"->load_data($class)
Truth table33%1/3
A ||= B
A= $dataB= "AI::MicroStructure"->load_data($class)
ABresult
1X1
011
000
16 2 ▶ croak "The optional argument to init() must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 0 2 2
17 if ref $data ne 'HASH';
18  
19 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ${"$class\::Theme"} = ( split /::/, $class )[-1];
22 2 @{"$class\::List"} = do { my %seen;
23 2 grep !$seen{$_}++, split /\s+/, $data->{names} };
24 2 *{"$class\::import"} = sub {
25 0 ▶ my $callpkg = caller(0);
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 my $structure = ${"$class\::Theme"};
27 0 my $meta = $class->new();
28 0 ▶ *{"$callpkg\::meta$structure"} = sub { $meta->name(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 2 };
30 2 ${"$class\::meta"} = $class->new();
31 }
32  
33 sub name {
34 6 ▶ my ( $self, $count ) = @_;
Subroutine name: called CC=8 SCAR=22.1
Pod documented
35 6 my $class = ref $self;
36  
37 6 ▶ if( ! $class ) { # called as a class method!
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 6 6
38 0 $class = $self;
39 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 0 $self = ${"$class\::meta"};
41 }
42  
43 6 ▶ if( defined $count && $count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count and $count == 0) 3 3 6
Condition100%3/3
defined $count and $count == 0
!ll&&!rl&&r
213
MC/DC100%2/2
defined $count and $count == 0
defined $count $count == 0
Truth table100%3/3
A and B
A= defined $countB= $count == 0
ABresult
0X0
100
111
44 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 return
46 3 ▶ wantarray ? shuffle @{"$class\::List"} : scalar @{"$class\::List"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
47 }
48  
49 3 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
12
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
50 3 my $list = $self->{cache};
51 {
52 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ▶ if (@{"$class\::List"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{"${class}::List";}) 0 3 3
54 0 ▶ push @$list, shuffle @{"$class\::List"} while @$list < $count;
Branch: 1 branch
BranchTrueFalseTotal
@$list < $count and do { push @$list, &shuffle(@{"${class}::List";}) } 0 0 0
55 }
56 }
57 3 splice( @$list, 0, $count );
58 }
59  
60 sub new {
61 4 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
62  
63 4 bless { cache => [] }, $class;
64 }
65  
66 sub structure {
67 0 ▶ my $class = ref $_[0] || $_[0];
Subroutine structure: not called CC=2 SCAR=11.6
Pod documented
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
68 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 0 return ${"$class\::Theme"};
70 }
71  
72 1 1;
73  
74 __END__
75  
76 =head1 NAME
77  
78 AI::MicroStructure::List - Base class for simple lists of names
79  
80 =head1 SYNOPSIS
81  
82 package AI::MicroStructure::beatles;
83 use AI::MicroStructure::List;
84 our @ISA = ( AI::MicroStructure::List );
85 __PACKAGE__->init();
86 1;
87  
88 =head1 NAME
89
90 AI::MicroStructure::beatles - The fab four structure
91
92 =head1 DESCRIPTION
93
94 Ladies and gentlemen, I<The Beatles>. I<(hysteric cries)>
95  
96 =cut
97
98 __DATA__
99 # names
100 john paul
101 george ringo
102  
103 =head1 DESCRIPTION
104  
105 C<AI::MicroStructure::List> is the base class for all structures that are
106 meant to return a random excerpt from a predefined list.
107  
108 =head1 METHOD
109  
110 AI::MicroStructure::List offers several methods, so that the subclasses
111 are easy to write (see full example in L<SYNOPSIS>):
112  
113 =over 4
114  
115 =item new()
116  
117 The constructor of a single instance. An instance will not repeat items
118 until the list is exhausted.
119  
120 =item init()
121  
122 init() must be called when the subclass is loaded, so as to read the
123 __DATA__ section and fully initialise it.
124  
125 =item name( $count )
126  
127 Return $count names (default: C<1>).
128  
129 Using C<0> will return the whole list in list context, and the size of the
130 list in scalar context.
131  
132 =item structure()
133  
134 Return the structure name.
135  
136 =back
137  
138 =head1 AUTHOR
139  
140 Philippe 'BooK' Bruhat, C<< <book@cpan.org> >>
141  
142 =head1 COPYRIGHT
143  
144 Copyright 2005-2012 Philippe 'BooK' Bruhat, All Rights Reserved.
145  
146 =head1 LICENSE
147  
148 This program is free software; you can redistribute it and/or modify it
149 under the same terms as Perl itself.
150  
151 =cut
152  
« lib/AI/MicroStructure/ObjectParser.pm ↑ Summary lib/AI/MicroStructure/WordBlacklist.pm »