File Coverage

blib/lib/Graph/Reader/TGF.pm
Criterion Covered Total %
statement 51 51 100.0
branch 14 14 100.0
condition 12 12 100.0
subroutine 9 9 100.0
pod n/a
total 86 86 100.0


line stmt bran cond sub pod time code
1             package Graph::Reader::TGF;
2              
3 4     4   250116 use base qw(Graph::Reader);
  4         10  
  4         2489  
4 4     4   241633 use strict;
  4         11  
  4         85  
5 4     4   16 use warnings;
  4         5  
  4         233  
6              
7 4     4   1749 use Encode qw(decode_utf8);
  4         41817  
  4         307  
8 4     4   1243 use Error::Pure qw(err);
  4         19775  
  4         491  
9              
10             our $VERSION = 0.04;
11              
12             # Edge callback.
13             sub _edge_callback {
14 5     5   17 my ($self, $graph, $id1, $id2, $edge_label) = @_;
15 5         144 $graph->set_edge_attribute($id1, $id2, 'label', $edge_label);
16 5         1102 return;
17             }
18              
19             # Init.
20             sub _init {
21 7     7   715689 my ($self, $param_hr) = @_;
22 7         54 $self->SUPER::_init();
23 7 100 100     82 if (exists $param_hr->{'edge_callback'}
      100        
24             && defined $param_hr->{'edge_callback'}
25             && ref $param_hr->{'edge_callback'} ne 'CODE') {
26              
27 1         4 err "Parameter 'edge_callback' isn't reference to code.";
28             }
29 6         30 $self->{'edge_callback'} = $param_hr->{'edge_callback'};
30 6 100 100     89 if (exists $param_hr->{'vertex_callback'}
      100        
31             && defined $param_hr->{'vertex_callback'}
32             && ref $param_hr->{'vertex_callback'} ne 'CODE') {
33              
34 1         7 err "Parameter 'vertex_callback' isn't reference to code.";
35             }
36 5         19 $self->{'vertex_callback'} = $param_hr->{'vertex_callback'};
37 5         14 return;
38             }
39              
40             # Read graph subroutine.
41             sub _read_graph {
42 6     6   14957 my ($self, $graph, $fh) = @_;
43 6         20 my $vertexes = 1;
44 6         446 while (my $line = decode_utf8(<$fh>)) {
45 24         421 chomp $line;
46              
47             # End of vertexes section.
48 24 100       131 if ($line =~ m/^#/ms) {
49 6         12 $vertexes = 0;
50 6         32 next;
51             }
52              
53             # Vertexes.
54 18 100       41 if ($vertexes) {
55 12         50 my ($id, $vertex_label) = split m/\s+/ms, $line, 2;
56 12 100       38 if (! defined $vertex_label) {
57 6         12 $vertex_label = $id;
58             }
59 12         49 $graph->add_vertex($id);
60 12 100       685 if ($self->{'vertex_callback'}) {
61 2         9 $self->{'vertex_callback'}->($self, $graph,
62             $id, $vertex_label);
63             } else {
64 10         33 $self->_vertex_callback($graph, $id,
65             $vertex_label);
66             }
67              
68             # Edges.
69             } else {
70 6         26 my ($id1, $id2, $edge_label) = split m/\s+/ms, $line, 3;
71 6         41 $graph->add_edge($id1, $id2);
72 6 100       904 if ($self->{'edge_callback'}) {
73 1         5 $self->{'edge_callback'}->($self, $graph, $id1,
74             $id2, $edge_label);
75             } else {
76 5         28 $self->_edge_callback($graph, $id1, $id2,
77             $edge_label);
78             }
79             }
80            
81             }
82 6         303 return;
83             }
84              
85             # Vertex callback.
86             sub _vertex_callback {
87 10     10   27 my ($self, $graph, $id, $vertex_label) = @_;
88 10         294 $graph->set_vertex_attribute($id, 'label', $vertex_label);
89 10         1077 return;
90             }
91              
92             1;
93              
94             __END__