Tarotplane/Cards.pm

filter: Statementstmt 16.4% 16 / 97 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 41.6% 5 / 12 Podpod 100% 6 / 6 totaltotal 15.2% 27 / 177 CC 25 SCAR 48.4
CC
25
Cov
11%
_read_cardfile
272.0
card_side
20.0
order_deck
20.0
CRAP 126.5
↑ Summary Tarotplane/UI.pm »
1 package App::Tarotplane::Cards;
2 1 our $VERSION = '2.02';
3 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 CARD_TERM => 0,
11 CARD_DEF => 1,
12 1 };
13  
14 0 ▶sub _read_cardfile {
Subroutine _read_cardfile: not called CC=16 SCAR=56.1
15  
16 0 my $file = shift;
17  
18 0 my @cards;
19  
20 my $fh;
21 0 ▶ if ($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 0 0
22 0 $fh = *STDIN;
23 } else {
24 0 ▶ open $fh, '<', $file or die "Could not open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "Could not open ${file}: $!\n" 0 0 0
25 }
26  
27 0 my $card = {};
28  
29 0 my $cn = 1;
30  
31 0 my $state = CARD_TERM;
32  
33 0 my $l = '';
34 0 ▶ while (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l) 0 0 0
35  
36 0 $l = readline $fh;
37  
38 0 ▶ if (not defined $l or $l eq "%\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $l or $l eq "%\n") 0 0 0
Condition0%0/3
not defined $l or $l eq "%\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $l or $l eq "%\n"
not defined $l $l eq "%\n"
Truth table0%0/3
not(A) or B
A= not defined $lB= $l eq "%\n"
ABresult
1X1
011
000
39  
40 # Blank cards are okay
41 0 ▶ next if not defined $card->{Term};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $card->{"Term"} 0 0 0
42  
43 0 ▶ die "No definition for card #$cn in $file\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $state == 1 0 0 0
44 unless $state == CARD_DEF;
45  
46 # Trim leading/trailing whitespace
47 0 $card->{Term} =~ s/^\s+|\s+$//g;
48 0 $card->{Definition} =~ s/^\s+|\s+$//g;
49  
50 # Truncate whitespace
51 0 $card->{Term} =~ s/\s+/ /g;
52 0 $card->{Definition} =~ s/\s+/ /g;
53  
54 ## Now interpret some escape codes
55 # '\:' -> ':'
56 0 $card->{Term} =~ s/\\:/:/g;
57 0 $card->{Definition} =~ s/\\:/:/g;
58  
59 # '\n' -> line break
60 0 $card->{Term} =~ s/\\n/\n/g;
61 0 $card->{Definition} =~ s/\\n/\n/g;
62  
63 # No longer need '\\' substitution null byte
64 0 $card->{Term} =~ s/\0//g;
65 0 $card->{Definition} =~ s/\0//g;
66  
67 0 push @cards, $card;
68 0 $card = {};
69 0 $state = CARD_TERM;
70  
71 0 $cn++;
72  
73 0 next;
74  
75 }
76  
77 # Skip comments and blanks
78 0 my $first = substr $l, 0, 1;
79 0 ▶ next if $first eq '#' or $first eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $first eq "#" or $first eq "\n" 0 0 0
Condition0%0/3
$first eq "#" or $first eq "\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
$first eq "#" or $first eq "\n"
$first eq "#" $first eq "\n"
Truth table0%0/3
A or B
A= $first eq "#"B= $first eq "\n"
ABresult
1X1
011
000
80  
81 # Substitute '\\' now so that '\\:' does not count as an escaped colon.
82 # The null byte is added so that the subsequent substitutions do not
83 # try to replace any '\\' escaped backslash.
84 0 $l =~ s/\\\\/\\\0/g;
85  
86 0 ▶ if ($state == CARD_TERM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0) { } 0 0 0
87 # Does card contain non-escaped colon?
88 0 ▶ if ($l =~ /(^|[^\\]):/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /(^|[^\\]):/u) { } 0 0 0
89 my (undef,
90 0 $te,
91 $de
92 ) = split(/(^.*[^\\]):/, $l, 2);
93 0 ▶ $card->{Term} .= $te || '';
Condition0%0/2
$te || ''
l!l
00
MC/DC0%0/1
$te || ''
$te
Truth table0%0/2
A
A= $te
Aresult
11
00
94 0 ▶ $card->{Definition} .= $de || '';
Condition0%0/2
$de || ''
l!l
00
MC/DC0%0/1
$de || ''
$de
Truth table0%0/2
A
A= $de
Aresult
11
00
95 0 $state = CARD_DEF;
96 } else {
97 0 $card->{Term} .= $l;
98 }
99 } else {
100 0 $card->{Definition} .= $l;
101 }
102  
103 }
104  
105 0 ▶ close $fh unless $fh eq *STDIN;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh eq *STDIN 0 0 0
106  
107 0 ▶ die "No cards found in $file\n" unless @cards;
Branch: 1 branch
BranchTrueFalseTotal
unless @cards 0 0 0
108  
109 0 return @cards;
110  
111 }
112  
113 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
114  
115 0 my $class = shift;
116 0 my @files = @_;
117  
118 0 my $self = {
119 Cards => [],
120 CardNum => 0,
121 };
122  
123 0 foreach my $f (@files) {
124  
125 0 my @cards = _read_cardfile($f);
126  
127 0 push @{$self->{Cards}}, @cards;
128 0 $self->{CardNum} += scalar @cards;
129  
130 }
131  
132 0 bless $self, $class;
133 0 return $self;
134  
135 }
136  
137 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
138  
139 0 my $self = shift;
140 0 my $get = shift;
141  
142 0 ▶ if (defined $self->{$get}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$get}) 0 0 0
143 0 return $self->{$get};
144 }
145  
146 0 return undef;
147  
148 }
149  
150 0 ▶sub card {
Subroutine card: not called CC=2 SCAR=17.9
Pod documented
151  
152 0 my $self = shift;
153 0 my $card = shift;
154  
155 0 ▶ return $self->{Cards}->[$card] // undef;
Condition0%0/2
$self->{"Cards"}[$card] // undef
l!l
00
MC/DC0%0/1
$self->{"Cards"}[$card] // undef
$self->{"Cards"}[$card]
Truth table0%0/2
A
A= $self->{"Cards"}[$card]
Aresult
11
00
156  
157 }
158  
159 0 ▶sub card_side {
Subroutine card_side: not called CC=4 SCAR=30.0
Pod documented
160  
161 0 my $self = shift;
162 0 my $card = shift;
163 0 my $side = shift;
164  
165 0 ▶ if ($side ne 'Term' and $side ne 'Definition') {
Branch: 1 branch
BranchTrueFalseTotal
if ($side ne "Term" and $side ne "Definition") 0 0 0
Condition0%0/3
$side ne "Term" and $side ne "Definition"
!ll&&!rl&&r
000
MC/DC0%0/2
$side ne "Term" and $side ne "Definition"
$side ne "Term" $side ne "Definition"
Truth table0%0/3
A and B
A= $side ne "Term"B= $side ne "Definition"
ABresult
0X0
100
111
166 0 die "side must be either 'Term' or 'Definition'";
167 }
168  
169 0 ▶ if (defined $self->{Cards}->[$card]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Cards"}[$card]) 0 0 0
170 0 return $self->{Cards}->[$card]->{$side};
171 }
172  
173 0 return undef;
174  
175 }
176  
177 0 ▶sub order_deck {
Subroutine order_deck: not called CC=4 SCAR=30.0
Pod documented
178  
179 0 my $self = shift;
180 0 ▶ my $by = shift // 'Term';
Condition0%0/2
shift() // "Term"
l!l
00
MC/DC0%0/1
shift() // "Term"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
181  
182 0 ▶ if ($by ne 'Term' and $by ne 'Definition') {
Branch: 1 branch
BranchTrueFalseTotal
if ($by ne "Term" and $by ne "Definition") 0 0 0
Condition0%0/3
$by ne "Term" and $by ne "Definition"
!ll&&!rl&&r
000
MC/DC0%0/2
$by ne "Term" and $by ne "Definition"
$by ne "Term" $by ne "Definition"
Truth table0%0/3
A and B
A= $by ne "Term"B= $by ne "Definition"
ABresult
0X0
100
111
183 0 die "Must order deck either by 'Term' or 'Definition'";
184 }
185  
186 0 @{$self->{Cards}} = sort {
187 0 $a->{$by} cmp $b->{$by}
188 0 } @{$self->{Cards}};
189  
190 }
191  
192 0 ▶sub shuffle_deck {
Subroutine shuffle_deck: not called CC=1 SCAR=6.9
Pod documented
193  
194 0 my $self = shift;
195  
196 0 @{$self->{Cards}} = shuffle @{$self->{Cards}};
197  
198 }
199  
200 1 1;
201  
202  
203  
204 =head1 NAME
205  
206 App::Tarotplane::Cards - Read tarotplane card files
207  
208 =head1 SYNOPSIS
209  
210 use App::Tarotplane::Cards;
211  
212 $deck = App::Tarotplane::Cards->new(@files);
213  
214 $deck->order_deck('Term');
215  
216 $deck->shuffle_deck();
217  
218 $card0 = $deck->card(0);
219  
220 $term1 = $deck->card_side(1, 'Term');
221  
222 =head1 DESCRIPTION
223  
224 App::Tarotplane::Cards is a module used by L<tarotplane> to read and organize
225 decks of card files from text files. For information on how to format card
226 files, consult the relevant documentation in the L<tarotplane> manual page.
227  
228 =head1 Object Methods
229  
230 =head2 App::Tarotplane::Cards->new(@files)
231  
232 Reads cards from @files, and returns an App::Tarotplane::Cards object. To read
233 more about the card file format, consult the L<tarotplane> manual page.
234  
235 =head2 $deck->get($get)
236  
237 Get $get from $deck. The following can be gotten:
238  
239 =over 4
240  
241 =item Cards
242  
243 Array ref of cards.
244  
245 =item CardNum
246  
247 Number of cards.
248  
249 =back
250  
251 Returns undef on failure.
252  
253 =head2 $deck->card($n)
254  
255 Return $n-th card from deck. The card will be a hash ref that looks like this
256  
257 {
258 Term => 'term string',
259 Definition => 'definition string',
260 }
261  
262 Returns undef on failure.
263  
264 =head2 $deck->card_side($n, $side)
265  
266 Returns side $side of card $n. $side must be 'Term' or 'Definition'
267 (case-sensitive).
268  
269 =head2 $deck->order_deck([$side])
270  
271 Order cards alphabetically by $side, which must be 'Term' or 'Definition'
272 (case-sensitive). If $side is not specified, sorts by terms.
273  
274 =head2 $deck->shuffle_deck()
275  
276 Randomize order of cards.
277  
278 =head1 AUTHOR
279  
280 Written by Samuel Young E<lt>L<samyoung12788@gmail.com>E<gt>.
281  
282 =head1 COPYRIGHT
283  
284 Copyright 2024-2026, Samuel Young
285  
286 This library is free software; you may redistribute it and/or
287 modify it under the same terms as Perl itself.
288  
289 =head1 SEE ALSO
290  
291 L<tarotplane>
292  
293 =cut
↑ Summary Tarotplane/UI.pm »