blib/lib/YAML/Shell.pm

filter: Statementstmt 22.6% 48 / 212 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 42.8% 12 / 28 Podpod 0.0% 0 / 13 totaltotal 15.2% 60 / 393 CC 68 SCAR 61.6
CC
68
Cov
14%
run
1178.7
handle_perl
119.2
handle_yaml
72.7
CRAP 472.1
↑ Summary
1 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package YAML::Shell;
3 2 our $VERSION = '0.70';
4  
5 2 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶sub Term::ReadLine::Perl::Tie::FIRSTKEY {undef}
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 $Data::Dumper::Indent = 1;
10 2 our $prompt = 'ysh > ';
11 2 my $round_trip = 0;
12 2 my $force = 0;
13 2 my $log = 0;
14 2 my $yaml_module = 'YAML::Any';
15 2 my $yaml_version;
16 2 $| = 1;
17 2 my $sh;
18  
19 sub run {
20 0 ▶ my $class = shift;
Subroutine run: not called CC=36 SCAR=70.7
Pod undocumented
21 0 set_version($yaml_module);
22 0 ▶ my @env_args = split /\s+/, ($ENV{YSH_OPT} || '');
Condition0%0/2
$ENV{'YSH_OPT'} || ''
l!l
00
MC/DC0%0/1
$ENV{'YSH_OPT'} || ''
$ENV{'YSH_OPT'}
Truth table0%0/2
A
A= $ENV{'YSH_OPT'}
Aresult
11
00
23 0 my @args = (@env_args, @_);
24 0 ▶ my $stream = -t STDIN ? '' : join('', <STDIN>);
Branch: 1 branch
BranchTrueFalseTotal
-t STDIN ? : 0 0 0
25 0 ▶ while (my $arg = shift @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @args) 0 0 0
26 0 ▶ set_version($1), next if $arg =~ /^-M(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^-M(.*)/ 0 0 0
27  
28 0 ▶ handle_help(), exit if $arg eq '-h';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-h" 0 0 0
29 0 ▶ handle_version(), exit if $arg eq '-v';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-v" 0 0 0
30 0 ▶ handle_Version(), exit if $arg eq '-V';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-V" 0 0 0
31  
32 0 ▶ $YAML::Indent = $1, next if $arg =~ /^-i(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^-i(\d+)$/ 0 0 0
33 0 ▶ $YAML::UseFold = 1, next if $arg eq '-uf';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-uf" 0 0 0
34 0 ▶ $YAML::UseBlock = 1, next if $arg eq '-ub';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-ub" 0 0 0
35 0 ▶ $YAML::UseCode = 1, next if $arg eq '-uc';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-uc" 0 0 0
36 0 ▶ $YAML::UseHeader = 0, next if $arg eq '-nh';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-nh" 0 0 0
37 0 ▶ $YAML::UseVersion = 0, next if $arg eq '-nv';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-nv" 0 0 0
38 0 ▶ $round_trip = 1, next if $arg eq '-r';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-r" 0 0 0
39 0 ▶ $round_trip = 2, next if $arg eq '-R';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-R" 0 0 0
40 0 ▶ $log = 1, next if $arg eq '-l';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-l" 0 0 0
41 0 ▶ $log = 2, next if $arg eq '-L';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-L" 0 0 0
42 0 ▶ $force = 1, next if $arg eq '-F';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-F" 0 0 0
43 0 warn(<<END), exit 1;
44 Unknown YAML Shell argument: '$arg'.
45 For help, try: perldoc ysh
46 END
47 }
48  
49 0 set_version($yaml_module);
50  
51 0 ▶ check_install() unless $force;
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
52  
53 0 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 0 0 0
54 0 ▶ if ($log == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log == 2) { } 0 0 0
55 0 ▶ open LOGFILE, "> ./ysh.log" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open LOGFILE, "> ./ysh.log" or die $! 0 0 0
56 }
57 else {
58 0 ▶ open LOGFILE, ">> ./ysh.log" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open LOGFILE, ">> ./ysh.log" or die $! 0 0 0
59 }
60 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 0 my $version = ${"${yaml_module}::VERSION"};
62 0 print LOGFILE "\n$yaml_module Version $version\n";
63 0 print LOGFILE "Begin logging at ", scalar localtime, "\n\n";
64 }
65  
66 0 ▶ if (not length($stream)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $stream) 0 0 0
67 0 Print(<<END);
68 Welcome to the YAML Test Shell. Type ':help' for more information.
69  
70 END
71 }
72  
73 {
74 {
75 0 local @ENV{qw(HOME EDITOR)};
76 0 local $^W;
77 0 $sh = Term::ReadLine::->new('The YAML Shell');
78 }
79  
80 sub my_readline {
81 0 ▶ print LOGFILE $prompt if $log;
Subroutine my_readline: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
82 0 my $input = $sh->readline($prompt);
83 0 ▶ if (not defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 0 0 0
84 0 $input = ':exit';
85 0 Print("\n");
86 }
87 0 $input .= "\n";
88 }
89 }
90  
91 0 ▶ if (length($stream)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stream) 0 0 0
92 0 my @objects;
93 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 0 eval { @objects = &{"${yaml_module}::Load"}($stream) };
95 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
96 0 print STDERR $@;
97 0 exit 1;
98 }
99 else {
100 0 print STDOUT Data::Dumper::Dumper(@objects);
101 0 exit 0;
102 }
103 }
104  
105 0 ▶ while ($_ = my_readline()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = my_readline()) 0 0 0
106 0 ▶ print LOGFILE $_ if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
107 0 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
108 0 ▶ exec('ysh', @ARGV) if /^\/$/;
Branch: 1 branch
BranchTrueFalseTotal
if m[^/$] 0 0 0
109 0 ▶ handle_command($_),next if /^:/;
Branch: 1 branch
BranchTrueFalseTotal
if /^:/ 0 0 0
110 0 ▶ handle_file($1),next if /^<\s*(\S+)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^<\s*(\S+)\s*$/ 0 0 0
111 0 ▶ handle_yaml($_),next if /^--\S/;
Branch: 1 branch
BranchTrueFalseTotal
if /^--\S/ 0 0 0
112 0 ▶ handle_yaml(''),next if /^===$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^===$/ 0 0 0
113 0 ▶ handle_perl($_,1),next if /^;/;
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 0 0 0
114 0 handle_perl($_,0),next;
115 0 Print("Unknown command. Type ':help' for instructions.\n");
116 }
117 }
118  
119 sub set_version {
120 0 ▶ my $module = shift;
Subroutine set_version: not called CC=2 SCAR=12.6
Pod undocumented
121 0 eval "require $module";
122 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
123 0 $yaml_module = $module;
124 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 0 $yaml_version = ${"${yaml_module}::VERSION"};
126 }
127  
128 sub Print {
129 0 ▶ print @_;
Subroutine Print: not called CC=2 SCAR=17.9
Pod undocumented
130 0 ▶ print LOGFILE @_ if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
131 }
132 0 ▶local $SIG{__WARN__} = sub { Print @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
133  
134 sub handle_file {
135 0 ▶ my ($file) = @_;
Subroutine handle_file: not called CC=2 SCAR=13.4
Pod undocumented
136 0 my @objects;
137 0 eval {
138 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
139 0 @objects = &{"${yaml_module}::LoadFile"}($file)
140 };
141 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
142 0 Print $@;
143 }
144 else {
145 0 Print Data::Dumper::Dumper(@objects);
146 }
147 }
148  
149 sub handle_perl {
150 0 ▶ my ($perl, $multi) = @_;
Subroutine handle_perl: not called CC=12 SCAR=47.8
Pod undocumented
151 0 my (@objects, $yaml, $yaml2);
152 0 local $prompt = 'perl> ';
153 0 my $line = '';
154 0 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) 0 0 0
155 0 ▶ while ($line !~ /^;$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^;$/) 0 0 0
156 0 $line = my_readline();
157 0 ▶ print LOGFILE $line if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
158 0 $perl .= $line;
159 }
160 }
161 0 @objects = eval "no strict;$perl";
162 0 ▶ Print("Bad Perl expression:\n$@"), return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
163 {
164 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
165 0 eval { $yaml = &{"${yaml_module}::Dump"}(@objects) };
166 }
167 0 ▶ $@ =~ s/^ at.*\Z//sm if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
168 0 ▶ Print("Dump failed:\n$@"), return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
169 0 Print $yaml;
170 0 ▶ if ($round_trip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($round_trip) 0 0 0
171 {
172 0 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 0 eval { $yaml2 = &{"${yaml_module}::Dump"}(&{"${yaml_module}::Load"}($yaml)) };
175 }
176 0 ▶ $@ =~ s/^ at.*\Z//sm if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
177 0 ▶ Print("Load failed:\n$@"), return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
178 0 ▶ if ($yaml eq $yaml2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml eq $yaml2) { } 0 0 0
179 0 ▶ if ($round_trip > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($round_trip > 1) 0 0 0
180 0 Print "\nData roundtripped OK!!!\n";
181 }
182 }
183 else {
184 0 Print "================\n";
185 0 Print "after roundtrip:\n";
186 0 Print "================\n";
187 # $yaml2 =~ s/ /_/g; #
188 # $yaml2 =~ s/\n/+/g; #
189 # Print $yaml2, "\n"; #
190 0 Print $yaml2;
191 0 Print "=========================\n";
192 0 Print "Data did NOT roundtrip...\n";
193 }
194 }
195 }
196  
197 sub handle_yaml {
198 0 ▶ my $yaml = shift;
Subroutine handle_yaml: not called CC=9 SCAR=42.9
Pod undocumented
199 0 my $line = $yaml;
200 0 my (@objects);
201 0 local $prompt = 'yaml> ';
202 0 $line = my_readline();
203 0 ▶ print LOGFILE $line if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
204 0 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
205 0 ▶ while ($line !~ /^\.{3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\.{3}$/) 0 0 0
206 0 $yaml .= $line;
207 0 $line = my_readline();
208 0 ▶ print LOGFILE $line if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
209 0 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
210 }
211 0 $yaml =~ s/\^{2,8}/\t/g;
212 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
213 0 eval { @objects = &{"${yaml_module}::Load"}($yaml) };
214 0 ▶ $@ =~ s/^ at.*\Z//sm if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
215 0 ▶ $@ =~ s/^/ /gm if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
216 0 ▶ Print("YAML Load Failed:\n$@"), return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
217 0 Print Data::Dumper::Dumper(@objects);
218 }
219  
220 sub handle_command {
221 0 ▶ my $line = shift;
Subroutine handle_command: not called CC=5 SCAR=34.0
Pod undocumented
222 0 chomp $line;
223 0 my ($cmd, $args);
224 0 ▶ if ($line =~ /^:(\w+)\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^:(\w+)\s*(.*)$/) 0 0 0
225 0 $cmd = $1;
226 0 $args = $2;
227 0 ▶ exit if $cmd =~ /^(exit|q(uit)?)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^(exit|q(uit)?)$/ 0 0 0
228 0 ▶ handle_help(),return if $cmd eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "help" 0 0 0
229 0 ▶ print `clear`,return if $cmd =~ /^c(lear)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^c(lear)?$/ 0 0 0
230 }
231 0 Print "Invalid command\n";
232 }
233  
234 sub handle_help {
235 0 ▶ Print <<END;
Subroutine handle_help: not called CC=1 SCAR=6.9
Pod undocumented
236 Welcome to the YAML Test Shell.
237  
238 When you to type in Perl, you get back YAML. And vice versa.
239  
240 By default, every line you type is a one line Perl program, the
241 return value of which will be displayed as YAML.
242  
243 To enter multi-line Perl code start the first line with ';' and use
244 as many lines as needed. Terminate with a line containing just ';'.
245  
246 To enter YAML text, start with a valid YAML separator/header line
247 which is typically '---'. Use '===' to indicate that there is no YAML
248 header. Enter as many lines as needed. Terminate with a line
249 containing just '...'.
250  
251 Shell Commands: (Begin with ':')
252 :exit or :q(uit) - leave the shell
253 :help - get this help screen
254  
255 END
256 }
257  
258 sub check_install {
259 0 ▶ if (-f "./YAML.pm" && -f "./pm_to_blib" &&
Subroutine check_install: not called CC=4 SCAR=30.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (-f "./YAML.pm" and -f "./pm_to_blib" and -M "./YAML.pm" < -M "./pm_to_blib") 0 0 0
Condition0%0/6
-f "./YAML.pm" and -f "./pm_to_blib" and -M "./YAML.pm" < -M "./pm_to_blib"
!ll&&!rl&&r
000
-f "./YAML.pm" and -f "./pm_to_blib"
!ll&&!rl&&r
000
MC/DC0%0/3
-f "./YAML.pm" and -f "./pm_to_blib" and -M "./YAML.pm" < -M "./pm_to_blib"
-f "./YAML.pm" -f "./pm_to_blib" -M "./YAML.pm" < -M "./pm_to_blib"
Truth table0%0/4
A and B and C
A= -f "./YAML.pm"B= -f "./pm_to_blib"C= -M "./YAML.pm" < -M "./pm_to_blib"
ABCresult
0XX0
10X0
1100
1111
260 -M "./YAML.pm" < -M "./pm_to_blib"
261 ) {
262 0 die "You need to 'make install'!\n";
263 }
264 }
265  
266 sub handle_version {
267 0 ▶ print STDERR <<END;
Subroutine handle_version: not called CC=1 SCAR=6.9
Pod undocumented
268  
269 ysh: '$VERSION'
270 ${yaml_module}: '$yaml_version'
271  
272 END
273 }
274  
275 sub handle_Version {
276 0 ▶ my $TRP = get_version('Term::ReadLine::Perl');
Subroutine handle_Version: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my $TRG = get_version('Term::ReadLine::Gnu');
278 0 my $POE = get_version('POE');
279 0 my $TO = get_version('Time::Object');
280  
281 0 print STDERR <<END;
282  
283 ysh: '$VERSION'
284 ${yaml_module}: '$yaml_version'
285 perl: '$Config::Config{version}'
286 Data::Dumper: '$Data::Dumper::VERSION'
287 Term::ReadLine::Perl: '$TRP'
288 Term::ReadLine::Gnu: '$TRG'
289 POE: '$POE'
290 Time::Object: '$TO'
291  
292 END
293 }
294  
295 sub get_version {
296 0 ▶ my ($module) = @_;
Subroutine get_version: not called CC=2 SCAR=17.9
Pod undocumented
297 0 my $version;
298 0 eval "no strict; use $module; \$version = \$${module}::VERSION";
299 #$version = "$@" if $@;
300 0 ▶ $version = "not installed" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
301 0 return $version;
302 }
303  
304 2 1;
↑ Summary