OPCS4/Parser.pm

filter: Statementstmt 100% 42 / 42 Branchbran 87.5% 7 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 95.3% 61 / 64 CC 7 SCAR 10.0
CC
7
Cov
96%
_get_parent
4.0
parse
4.0
BEGIN
1.0
CRAP 2.7
« OPCS4.pm ↑ Summary OPCS4/Graph.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Medical::OPCS4::Parser;
5 # ABSTRACT: A parser object.
6  
7 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Medical::OPCS4::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Medical::OPCS4::Parser - OPCS4 Parser object
13  
14 =head1 METHODS
15  
16 =head2 new
17  
18 Create a new parser object.
19
20 Do not use directly.
21  
22 =cut
23  
24 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
25 1 my $class = shift;
26 1 my $self = { };
27
28 $self->{csv} =
29 1 Text::CSV->new({ 'sep_char' => "," });
30
31 1 $self->{csv}->column_names( qw( opcs description) );
32
33 $self->{g} =
34 1 Medical::OPCS4::Graph->new;
35
36 1 return bless $self, $class;
37
38 }
39  
40 =head2 parse
41  
42 The main parser function. Accepts a tab separated file of OPCS4 codes
43 along with their descriptions and parses it.
44  
45 Returns true on success and undef on failure.
46  
47 =cut
48  
49 1 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
50 1 my $self = shift;
51 1 my $filename = shift;
52
53 1 ▶ open my $io, "<:encoding(utf8)", $filename
Condition50%1/2
$filename || die("${filename}: $!")
l!l
10
MC/DC0%0/1
$filename || die("${filename}: $!")
$filename
Truth table50%1/2
A
A= $filename
Aresult
11
00
54 || die "$filename: $!";
55  
56 ##
57 ## First pass: add all the nodes
58
59 1 ▶ while ( my $rh = $self->{csv}->getline_hr( $io) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rh = $self->{"csv"}->getline_hr($io)) 5 1 6
60 5 my $opcs = $rh->{opcs};
61 5 $self->{g}->add_vertex( $rh->{opcs} );
62 5 $self->{g}->set_vertex_attribute( $rh->{opcs}, 'description', $rh->{description} );
63 }
64
65 ##
66 ## Second pass: add all the edges
67
68 1 my @vertices = $self->{g}->vertices;
69
70 1 foreach my $vertex ( @vertices ) {
71 6 my $parent = $self->_get_parent( $vertex );
72 6 $self->{g}->add_edge( $parent, $vertex );
73 }
74  
75 1 return $self->{g};
76
77 }
78  
79 =head2 _get_parent
80  
81 Internal parser function used to discover the parent
82 of each node.
83
84 Do not use directly.
85  
86 =cut
87  
88 6 ▶sub _get_parent {
Subroutine _get_parent: called CC=4 SCAR=13.9
89 6 my $self = shift;
90 6 my $term = shift;
91  
92 6 ▶ if ( $term eq 'root' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term eq "root") 1 5 6
93 1 return 'root';
94 }
95  
96 5 my $length = length( $term );
97
98 5 ▶ if ( $length == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 3) 1 4 5
99 1 return 'root'
100 }
101
102 4 ▶ if ( $term =~ m/\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term =~ /\./) 4 0 4
103 4 return substr( $term, 0, ( index $term, '.') );
104 }
105
106 }
107  
108 =head2 graph
109  
110 Returns the internal Medical::OPCS4::Graph object.
111  
112 =cut
113  
114 32 ▶sub graph {
Subroutine graph: called CC=1 SCAR=0.0
Pod documented
115 32 my $self = shift;
116 32 return $self->{g};
117 }
118  
119 1 1;
« OPCS4.pm ↑ Summary OPCS4/Graph.pm »