TT/Sort.pm

filter: Statementstmt 46.7% 29 / 62 Branchbran 6.2% 1 / 16 Conditioncond 7.4% 2 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 33.3% 6 / 18 Podpod 0.0% 0 / 12 totaltotal 25.0% 38 / 152 CC 15 SCAR 23.5
CC
15
Cov
30%
cmdfh
30.0
tmpfile
12.0
vmsg
12.0
CRAP 10.4
« TT/DBx/Field.pm ↑ Summary TT/DBx/Enum.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: Lingua::TT::Sort.pm
3 ## Author: Bryan Jurish <TT/IO.pm>
4 ## Descript: TT: sorting (using system 'sort' cmd)
5  
6  
7 package Lingua::TT::Sort;
8 1 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ##==============================================================================
15 ## Globals & Constants
16  
17 1 our @ISA = qw(Exporter);
18 1 our @EXPORT = qw();
19 1 our %EXPORT_TAGS = (
20 'vars' => [qw($FS_KEEP $FS_DIR $FS_SUFFIX $FS_PREFIX $FS_VERBOSE)],
21 'sort' => [qw(fs_filesort fs_filemerge fs_cmdfh)],
22 'tmp' => [qw(fs_tmpfile fs_system)],
23 );
24 1 $EXPORT_TAGS{all} = [map {@$_} values(%EXPORT_TAGS)];
25 1 our @EXPORT_OK = @{$EXPORT_TAGS{all}};
26  
27  
28 1 our $FS_KEEP = 0;
29 1 ▶our $FS_DIR = $ENV{TMPDIR} || $ENV{TMP} || (-d '/tmp' ? '/tmp' : '.');
Branch: 1 branch
BranchTrueFalseTotal
-d '/tmp' ? : 1 0 1
Condition33%2/6
$ENV{'TMPDIR'} || $ENV{'TMP'} || (-d '/tmp' ? '/tmp' : '.')
l!l&&r!l&&!r
010
$ENV{"TMPDIR"} or $ENV{"TMP"}
l!l&&r!l&&!r
001
MC/DC0%0/3
$ENV{'TMPDIR'} || $ENV{'TMP'} || (-d '/tmp' ? '/tmp' : '.')
$ENV{"TMPDIR"} $ENV{"TMP"} (-d '/tmp' ? '/tmp' : '.')
Truth table25%1/4
A or B || C
A= $ENV{"TMPDIR"}B= $ENV{"TMP"}C= (-d '/tmp' ? '/tmp' : '.')
ABCresult
1XX1
01X1
0011
0000
30 1 our $FS_SUFFIX = '.tmp';
31 1 our $FS_PREFIX = 'fs';
32 1 our $FS_VERBOSE = 0;
33  
34 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 $ENV{LC_ALL}="C";
36 }
37  
38 ##==============================================================================
39 ## OO interface
40  
41 ## $sort = Lingua::TT::Sort->new(%opts)
42 ## + %opts
43 ## keep => $bool, ##-- keep temps?
44 ## prefix => $prefix, ##-- tempfile prefix
45 ## suffix => $suffix, ##-- tempfile suffix
46 ## dir => $dir, ##-- temp directory
47 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $that = shift;
49 0 ▶ return bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
50 keep=>$FS_KEEP,
51 dir=>$FS_DIR,
52 prefix=>$FS_PREFIX,
53 suffix=>$FS_SUFFIX,
54 verbose=>$FS_VERBOSE,
55 @_
56 }, ref($that)||$that);
57 }
58  
59  
60 ##==============================================================================
61 ## Methods
62  
63 ## $tmpfile = $sort->tmpfile()
64 0 ▶sub tmpfile {
Subroutine tmpfile: not called CC=3 SCAR=24.8
Pod undocumented
65 0 my $sort = shift;
66 my ($tmpfh,$tmpfile) = tempfile($sort->{prefix}.('X' x (length($sort->{prefix}) <= 4 ? (8-length($sort->{prefix})) : 4)),
67 SUFFIX=>$sort->{suffix},
68 UNLINK=>!$sort->{keep},
69 0 ▶ DIR=>$sort->{dir});
Branch: 1 branch
BranchTrueFalseTotal
length $sort->{'prefix'} <= 4 ? : 0 0 0
70 0 ▶ confess(ref($sort)."::tmpfile(): could not get temporary file: $!") if (!defined($tmpfh));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmpfh 0 0 0
71 0 $tmpfh->close;
72 0 return $tmpfile;
73 }
74  
75 ## undef = $sort->vmsg($level,@msg)
76 # + print @msg to STDERR if $sort->{verbose} >= $level
77 0 ▶sub vmsg {
Subroutine vmsg: not called CC=3 SCAR=24.8
Pod undocumented
78 0 my ($sort,$level,@msg) = @_;
79 0 ▶ print STDERR (@msg) if (defined($sort->{verbose}) && $sort->{verbose} >= $level);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sort->{"verbose"} and $sort->{"verbose"} >= $level 0 0 0
Condition0%0/3
defined $sort->{"verbose"} and $sort->{"verbose"} >= $level
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sort->{"verbose"} and $sort->{"verbose"} >= $level
defined $sort->{"verbose"} $sort->{"verbose"} >= $level
Truth table0%0/3
A and B
A= defined $sort->{"verbose"}B= $sort->{"verbose"} >= $level
ABresult
0X0
100
111
80 }
81  
82  
83 ## $rc = $sort->system(@cmd)
84 0 ▶sub system {
Subroutine system: not called CC=2 SCAR=17.9
Pod undocumented
85 0 my ($sort,@cmd) = @_;
86 0 ▶ $sort->vmsg(1,(ref($sort)||$sort), ": ", join(' ', @cmd), "\n");
Condition0%0/3
ref $sort || $sort
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $sort || $sort
ref $sort $sort
Truth table0%0/3
A || B
A= ref $sortB= $sort
ABresult
1X1
011
000
87 0 return CORE::system(@cmd);
88 }
89  
90 ## $tmpfile = $sort->filesort(@sort_args)
91 0 ▶sub filesort {
Subroutine filesort: not called CC=3 SCAR=24.8
Pod undocumented
92 0 my $sort = shift;
93 0 my $tmpfile = fs_tmpfile();
94 0 my @cmd = ('sort','-o',$tmpfile,@_);
95 0 ▶ $sort->system(@cmd)==0
Branch: 1 branch
BranchTrueFalseTotal
$sort->system(@cmd) == 0 or confess(ref $sort || $sort, ': system(' . join(" ", @cmd) . ") failed: $!") 0 0 0
Condition0%0/3
ref $sort || $sort
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $sort || $sort
ref $sort $sort
Truth table0%0/3
A || B
A= ref $sortB= $sort
ABresult
1X1
011
000
96 or confess(ref($sort)||$sort, ": system(".join(' ',@cmd).") failed: $!");
97 0 return $tmpfile;
98 }
99  
100 ## $tmpfile = $sort->filemerge(@infiles)
101 ## + @infiles are assumed already to be sorted
102 0 ▶sub filemerge {
Subroutine filemerge: not called CC=3 SCAR=24.8
Pod undocumented
103 0 my $sort = shift;
104 0 my $tmpfile = fs_tmpfile();
105 0 my @cmd = ('sort','-o',$tmpfile,'-m',@_);
106 0 ▶ $sort->system(@cmd)==0
Branch: 1 branch
BranchTrueFalseTotal
$sort->system(@cmd) == 0 or confess(ref $sort || $sort, ': system(' . join(" ", @cmd) . ") failed: $!") 0 0 0
Condition0%0/3
ref $sort || $sort
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $sort || $sort
ref $sort $sort
Truth table0%0/3
A || B
A= ref $sortB= $sort
ABresult
1X1
011
000
107 or confess(ref($sort)||$sort, ": system(".join(' ',@cmd).") failed: $!");
108 0 return $tmpfile;
109 }
110  
111 ## $sortfh = $sort->cmdfh(@cmd)
112 ## + warning: @cmd get interpolated; you may need to shell-quote them!
113 ## + opend for read from `@cmd` by default
114 0 ▶sub cmdfh {
Subroutine cmdfh: not called CC=5 SCAR=34.0
Pod undocumented
115 0 my ($sort,@cmd) = @_;
116 0 my $cmd = join(' ', @cmd);
117 0 ▶ $cmd = "$cmd |" if ($cmd !~ /\|/);
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /\|/ 0 0 0
118 0 ▶ $sort->vmsg(1,(ref($sort)||$sort), ": $cmd\n");
Condition0%0/3
ref $sort || $sort
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $sort || $sort
ref $sort $sort
Truth table0%0/3
A || B
A= ref $sortB= $sort
ABresult
1X1
011
000
119 0 ▶ my $sortfh = IO::File->new($cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $sortfh = "IO::File"->new($cmd) or confess(ref $sort || $sort, ": open failed for `$cmd`: $!") 0 0 0
Condition0%0/3
ref $sort || $sort
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $sort || $sort
ref $sort $sort
Truth table0%0/3
A || B
A= ref $sortB= $sort
ABresult
1X1
011
000
120 or confess((ref($sort)||$sort), ": open failed for \`$cmd\`: $!");
121 0 return $sortfh;
122 }
123  
124  
125 ##==============================================================================
126 ## Utils
127  
128 ## $tmpfile = PACKAGE::tmpfile()
129 0 ▶sub fs_tmpfile { return __PACKAGE__->new->tmpfile; }
Subroutine fs_tmpfile: not called CC=1 SCAR=6.9
Pod undocumented
130  
131 ## $sorted_filename = PACKAGE::fs_filesort(@sort_args)
132 0 ▶sub fs_filesort { return __PACKAGE__->new->filesort(@_); }
Subroutine fs_filesort: not called CC=1 SCAR=6.9
Pod undocumented
133  
134 ## $tmpfile = PACKAGE::fs_filemerge(@infiles)
135 ## + @infiles are assumed already to be sorted
136 0 ▶sub fs_filemerge { return __PACKAGE__->new->filemerge(@_); }
Subroutine fs_filemerge: not called CC=1 SCAR=6.9
Pod undocumented
137  
138 ## $cmdfh = PACKAGE::fs_cmdfh($cmd)
139 0 ▶sub fs_cmdfh { return __PACKAGE__->new->cmdfh(@_); }
Subroutine fs_cmdfh: not called CC=1 SCAR=6.9
Pod undocumented
140  
141 ## $rc = PACKAGE::fs_system(@cmd)
142 0 ▶sub fs_system { return __PACKAGE__->new->system(@_); }
Subroutine fs_system: not called CC=1 SCAR=6.9
Pod undocumented
143  
144  
145 ##==============================================================================
146 ## Footer
147 1 1;
148  
149 __END__
« TT/DBx/Field.pm ↑ Summary TT/DBx/Enum.pm »