blib/lib/Sort/TSort.pm

filter: Statementstmt 100% 29 / 29 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 95.3% 41 / 43 CC 4 SCAR 7.9
CC
4
Cov
97%
tsort
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.2
↑ Summary
1 =head1 NAME
2  
3 Sort::TSort - topological sort; a very simple wrapper around 'tsort' command line utility.
4  
5 =head1 SYNOPSIS
6  
7 use Sort::TSort qw(tsort);
8 my $sorted_items = toposort($ordered_pairs);
9  
10 =head1 DESCRIPTION
11  
12 Sort::TSort::tsort performs a topological sort of an acyclic directed graph defined by list of edges (pairs of vertices).
13 Sort::TSort does this by invoking external command tsort (1).
14  
15 Exports one subroutine on demand: tsort.
16  
17 If external program tsort is not installed, the subroutine dies.
18  
19 If there are cycles in the graph, the subroutine dies.
20  
21 If an item of input array has any other length than 2, the subroutine dies (this behaviour is different from external tsort, which accept any even number of items per row).
22  
23 Sort::TSort tries to do its best to deal correctly with non-ASCII data (by encoding input strings in utf-8 and decoding result from utf-8).
24  
25 =head1 EXAMPLE
26  
27  
28 use Sort::TSort qw/tsort/;
29 my $partial_order = [
30 [ 'a', 'b' ],
31 [ 'a', 'c' ],
32 [ 'c', 'x' ],
33 [ 'b', 'x' ],
34 [ 'x', 'y' ],
35 [ 'y', 'z' ],
36 ];
37  
38 my $sorted = tsort($partial_order);
39  
40 # Result:
41 # $sorted = [
42 # 'a',
43 # 'c',
44 # 'b',
45 # 'x',
46 # 'y',
47 # 'z'
48 # ];
49  
50  
51 my $partial_order = [
52 [ 'a', 'b' ],
53 [ 'b', 'c' ],
54 [ 'c', 'a' ],
55 ];
56  
57 my $sorted = tsort($partial_order);
58  
59 # Result: tsort dies (there is a cycle 'a-b-c')
60  
61  
62 my $partial_order = [
63 [ 'a', 'b', 'c', 'd' ],
64 [ 'b', 'c' ],
65 [ 'c', 'a' ],
66 ];
67  
68 my $sorted = tsort($partial_order);
69  
70 # Result: tsort dies (first row doesn't have exactly 2 items)
71  
72 =head1 CAVEATS
73  
74 Requires external tsort program to be installed.
75  
76 Not tested on Windows (probably it can be made to work there with tsort from Perl Power Tools).
77  
78 Not intended to sort very large arrays (input and output data are copied multiple times, join'ed and split'ed, which may lead to unnecessary memory consumption).
79  
80  
81 =head1 SEE ALSO
82  
83 tsort (1), tsort from PerlPowerTools, L<Sort::Topological>, L<tcsort>, L<Algorithm::TSort>
84  
85 =head1 AUTHOR
86  
87 Elena Bolshakova <helena@cpan.org>
88  
89 =cut
90  
91 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 package Sort::TSort;
96  
97 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use IPC::Cmd qw(can_run run_forked);
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 1 our @EXPORT_OK = qw(
103 tsort
104 );
105  
106  
107 sub tsort
108 16 ▶{
Subroutine tsort: called CC=4 SCAR=13.9
Pod undocumented
109 16 my ($arr) = @_;
110  
111 16 ▶ can_run('tsort') or die 'tsort is not installed, stop';
Branch: 1 branch
BranchTrueFalseTotal
can_run("tsort") or die "tsort is not installed, stop" 16 0 16
112  
113 16 ▶ my $data = join "\n", map { @$_ == 2 ? join " ", @$_ : die "unexpected number of elements in a row: [".join(", ", @$_)."]" } @$arr;
Branch: 1 branch
BranchTrueFalseTotal
@$_ == 2 ? : 43 5 48
114  
115 10 my $res = run_forked( 'tsort', { child_stdin => Encode::encode_utf8($data), } );
116  
117 10 ▶ if ( $res->{exit_code} != 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"exit_code"} != 0) 4 6 10
118 4 die "tsort exited with non-zero exit code ($res->{exit_code})\n$res->{stderr}";
119 }
120  
121 6 my @sorted_array = split "\n", Encode::decode_utf8($res->{stdout});
122  
123 6 return \@sorted_array;
124 }
125  
126  
127 1 1;
128  
↑ Summary