blib/lib/Proc/InvokeEditor.pm

filter: Statementstmt 58.4% 93 / 159 Branchbran 31.4% 17 / 54 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 82.3% 14 / 17 Podpod 100% 8 / 8 totaltotal 52.3% 135 / 258 CC 36 SCAR 35.9
CC
36
Cov
50%
_edit
132.0
first_usable
22.7
edit
20.0
CRAP 36.3
↑ Summary
1 package Proc::InvokeEditor;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IPC::Cmd qw(can_run);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 File::Temp->safe_level( File::Temp::HIGH );
12  
13 2 require Exporter;
14  
15 2 ▶use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 @DEFAULT_EDITORS);
17  
18 2 @ISA = qw(Exporter);
19  
20 # Items to export into callers namespace by default. Note: do not export
21 # names by default without a very good reason. Use EXPORT_OK instead.
22 # Do not simply export all your public functions/methods/constants.
23  
24 # This allows declaration use Proc::InvokeEditor ':all';
25 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
26 # will save memory.
27 2 %EXPORT_TAGS = ( 'all' => [ qw(
28
29 ) ] );
30  
31 2 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
32  
33 2 @EXPORT = qw(
34
35 );
36 2 $VERSION = '1.13';
37  
38 @DEFAULT_EDITORS = ( $ENV{'VISUAL'}, $ENV{'EDITOR'}, '/usr/bin/vi',
39 '/bin/vi', '/bin/ed',
40 2 map({ can_run($_) } qw(vi ed notepad.exe))
41 );
42  
43 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.3
Pod documented
44 1 my $proto = shift;
45 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
46 1 my $self = {
47 'editors' => \@DEFAULT_EDITORS,
48 'cleanup' => 1,
49 'keep_file' => 0,
50 };
51 1 ▶ croak("$class requires an even number of parameters") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
52 1 my %args = @_;
53 1 foreach my $param (qw(editors cleanup keep_file)) {
54 3 ▶ if ($args{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$param}) 0 3 3
55 0 $self->{$param} = $args{$param};
56 }
57 }
58 1 bless ($self, $class);
59 1 return $self;
60 }
61  
62 4 ▶sub editors {
Subroutine editors: called CC=2 SCAR=6.9
Pod documented
63 4 my $self = shift;
64 4 my $editors = shift;
65 4 ▶ if (defined $editors) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $editors) 1 3 4
66 1 assert(ref($editors) eq 'ARRAY');
67 1 $self->{'editors'} = $editors;
68 }
69 4 return $self->{'editors'};
70 }
71  
72 1 ▶sub editors_prepend {
Subroutine editors_prepend: called CC=1 SCAR=0.0
Pod documented
73 1 my $self = shift;
74 1 my $edit = shift;
75 1 assert(ref($edit) eq 'ARRAY');
76 1 my @editors = @{$self->{'editors'}};
77 1 unshift @editors, @$edit;
78 1 $self->{'editors'} = \@editors;
79 }
80  
81 1 ▶sub editors_env {
Subroutine editors_env: called CC=5 SCAR=16.5
Pod documented
82 1 my $self = shift;
83 1 my $edit = shift;
84 1 assert(ref($edit) eq 'ARRAY');
85 1 my @editors;
86 1 ▶ if (@$edit) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$edit) 1 0 1
87 1 foreach my $e (@$edit) {
88 2 ▶ if (exists $ENV{$e} and defined $ENV{$e}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$e} and defined $ENV{$e}) 2 0 2
Condition33%1/3
exists $ENV{$e} and defined $ENV{$e}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $ENV{$e} and defined $ENV{$e}
exists $ENV{$e} defined $ENV{$e}
Truth table33%1/3
A and B
A= exists $ENV{$e}B= defined $ENV{$e}
ABresult
0X0
100
111
89 2 push @editors, $ENV{$e};
90 }
91 }
92 1 my @editors_list = @{$self->{'editors'}};
93 1 unshift @editors_list, @editors;
94 1 $self->{'editors'} = \@editors_list;
95 }
96 1 return $self->{'editors'};
97 }
98  
99 2 ▶sub cleanup {
Subroutine cleanup: called CC=2 SCAR=6.9
Pod documented
100 2 my $self = shift;
101 2 my $cleanup = shift;
102 2 ▶ $self->{'cleanup'} = $cleanup if defined $cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cleanup 1 1 2
103 2 return $self->{'cleanup'};
104 }
105  
106 0 ▶sub keep_file {
Subroutine keep_file: not called CC=2 SCAR=17.9
Pod documented
107 0 my $self = shift;
108 0 my $keep_file = shift;
109 0 ▶ $self->{'keep_file'} = $keep_file if defined $keep_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $keep_file 0 0 0
110 0 return $self->{'keep_file'};
111 }
112  
113 0 ▶sub edit {
Subroutine edit: not called CC=4 SCAR=30.0
Pod documented
114 0 my $self = shift;
115 0 my $arg = shift;
116 0 my $suff = shift;
117 # if the argument supplied is a reference to an array of lines,
118 # join it together based on the input record separator
119 0 ▶ if (ref($arg) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq "ARRAY") 0 0 0
120 0 $arg = join $/, @$arg;
121 }
122 0 my $result;
123 0 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
124 ($result, $self->{'filename'}) = _edit(
125 $arg,
126 $self->{'editors'},
127 $self->{'cleanup'},
128 $self->{'keep_file'},
129 0 $self->{'filename'},
130 $suff,
131 );
132 } else {
133 0 ($result) = _edit($arg, \@DEFAULT_EDITORS, 1, 0, undef, $suff);
134 }
135 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
136 0 my @result = split m|$/|, $result;
137 0 return @result;
138 } else {
139 0 return $result;
140 }
141 }
142  
143 1 ▶sub first_usable {
Subroutine first_usable: called CC=12 SCAR=31.2
Pod documented
144 1 my $self = shift;
145 1 my $er = shift;
146 1 my @editors;
147 1 ▶ if (defined $er) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $er) { } 0 1 1
148 0 @editors = @$er;
149 } else {
150 1 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 1 1
151 0 @editors = @{$self->{'editors'}};
152 } else {
153 1 @editors = @DEFAULT_EDITORS;
154 }
155 }
156  
157 1 my $chosen_editor;
158 1 my @path = File::Spec->path;
159 1 EDITORS: foreach my $editor (@editors) {
160 8 ▶ next unless defined $editor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $editor 5 3 8
161 3 my @editor_bits;
162 3 ▶ if( $^O =~ /mswin/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /mswin/i) { } 0 3 3
163 0 require Text::ParseWords;
164 0 $editor =~ s!\\!\\\\!g; # quote path for shellwords
165 0 @editor_bits = Text::ParseWords::shellwords($editor);
166 } else {
167 3 @editor_bits = split /\s+/, $editor;
168 };
169 3 ▶ next unless defined $editor_bits[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $editor_bits[0] 0 3 3
170 3 ▶ if (File::Spec->file_name_is_absolute($editor_bits[0])
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($editor_bits[0]) and -x $editor_bits[0]) { } 0 3 3
Condition33%1/3
"File::Spec"->file_name_is_absolute($editor_bits[0]) and -x $editor_bits[0]
!ll&&!rl&&r
030
MC/DC0%0/2
"File::Spec"->file_name_is_absolute($editor_bits[0]) and -x $editor_bits[0]
"File::Spec"->file_name_is_absolute($editor_bits[0]) -x $editor_bits[0]
Truth table33%1/3
A and B
A= "File::Spec"->file_name_is_absolute($editor_bits[0])B= -x $editor_bits[0]
ABresult
0X0
100
111
171 and -x $editor_bits[0]) {
172 0 $chosen_editor = \@editor_bits;
173 0 last;
174 } else {
175 3 foreach my $dir (@path) {
176 30 my $file = File::Spec->catfile($dir, $editor_bits[0]);
177 30 ▶ if (-x $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $file) 0 30 30
178 0 $editor_bits[0] = $file;
179 0 $chosen_editor = \@editor_bits;
180 0 last EDITORS;
181 }
182 }
183 }
184 }
185 1 ▶ die "Couldn't find an editor: $!" unless defined $chosen_editor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chosen_editor 1 0 1
186  
187 0 return $chosen_editor;
188 }
189  
190 0 ▶sub _edit {
Subroutine _edit: not called CC=11 SCAR=48.8
191 0 my $string = shift;
192 0 my $er = shift;
193 0 my $unlink = shift;
194 0 my $keep_file = shift;
195 0 my $filename = shift;
196 0 my $suff = shift;
197  
198 0 assert(ref($er) eq 'ARRAY');
199 0 assert(defined $unlink);
200 0 my @editors = @$er;
201 # Find an editor
202  
203 0 my $chosen_editor = first_usable(undef, $er);
204  
205 0 my @suff;
206 0 ▶ @suff = (SUFFIX => $suff) if $suff;
Branch: 1 branch
BranchTrueFalseTotal
if $suff 0 0 0
207  
208 # get a temp file, and write the text to it
209 0 ▶ if (defined($filename) && $keep_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and $keep_file) { } 0 0 0
Condition0%0/3
defined $filename and $keep_file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filename and $keep_file
defined $filename $keep_file
Truth table0%0/3
A and B
A= defined $filenameB= $keep_file
ABresult
0X0
100
111
210 0 ▶ open my $fh, '>', $filename or die "Couldn't open tempfile [$filename]; $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Couldn't open tempfile [$filename]; $!" 0 0 0
211 0 print $fh $string;
212 0 ▶ close $fh or die "Couldn't close tempfile [$filename]; $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Couldn't close tempfile [$filename]; $!" 0 0 0
213 }
214 else {
215 0 my $fh;
216 0 ($fh, $filename) = tempfile(UNLINK => $unlink, @suff);
217 0 print $fh $string;
218 0 ▶ close $fh or die "Couldn't close tempfile [$filename]; $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Couldn't close tempfile [$filename]; $!" 0 0 0
219 }
220 # start the editor
221 0 my $rc = system @$chosen_editor, $filename;
222 # check what happened - die if it all went wrong.
223 0 ▶ unless ($rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc == 0) 0 0 0
224 0 my ($exit_value, $signal_num, $dumped_core);
225 0 $exit_value = $? >> 8;
226 0 $signal_num = $? & 127;
227 0 $dumped_core = $? & 128;
228 0 die "Error in editor - exit val = $exit_value, signal = $signal_num, coredump? = $dumped_core: $!";
229 }
230  
231 # read the temp file
232 0 ▶ sysopen(FH, $filename, O_RDONLY) or die "Couldn't sysopen $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen FH, $filename, 0 or die "Couldn't sysopen ${filename}: $!" 0 0 0
233 0 my $result;
234 0 { local $/; $result = <FH>; }
235 0 ▶ close FH or die "Couldn't close [$filename]: $!";
Branch: 1 branch
BranchTrueFalseTotal
close FH or die "Couldn't close [$filename]: $!" 0 0 0
236 # return as string
237 0 ▶ if ($keep_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_file) { } 0 0 0
238 0 return ($result, $filename);
239 }
240 else {
241 0 return ($result);
242 }
243 }
244  
245 2 1;
246 __END__
↑ Summary