lib/Inline/CLIPS.pm

filter: Statementstmt 48.8% 42 / 86 Branchbran 20.0% 6 / 30 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 5 totaltotal 40.3% 67 / 166 CC 27 SCAR 26.4
CC
27
Cov
40%
run_file
30.1
_alien_executable
30.0
run_program
20.0
CRAP 14.1
↑ Summary blib/lib/Inline/CLIPS.pm »
1 package Inline::CLIPS;
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 Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.001';
13  
14 sub new {
15 3 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 my $self = bless {
17 executable => $args{executable},
18 library => $args{library},
19 3 }, $class;
20 3 return $self;
21 }
22  
23 sub executable {
24 1 ▶ my ($self) = @_;
Subroutine executable: called CC=5 SCAR=19.4
Pod undocumented
25 1 ▶ return $self->{executable} if defined $self->{executable};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"executable"} 0 1 1
26  
27 $self->{executable} = $ENV{INLINE_CLIPS_EXECUTABLE}
28 1 ▶ || _path_executable('clips')
Condition37%3/8
$ENV{'INLINE_CLIPS_EXECUTABLE'} || _path_executable('clips') || _alien_executable() || ''
l!l
10
$ENV{"INLINE_CLIPS_EXECUTABLE"} or _path_executable("clips") or _alien_executable()
l!l&&r!l&&!r
100
$ENV{"INLINE_CLIPS_EXECUTABLE"} or _path_executable("clips")
l!l&&r!l&&!r
100
MC/DC0%0/3
$ENV{'INLINE_CLIPS_EXECUTABLE'} || _path_executable('clips') || _alien_executable() || ''
$ENV{"INLINE_CLIPS_EXECUTABLE"} _path_executable("clips") _alien_executable()
Truth table25%1/4
A or B or C
A= $ENV{"INLINE_CLIPS_EXECUTABLE"}B= _path_executable("clips")C= _alien_executable()
ABCresult
1XX1
01X1
0011
0000
29 || _alien_executable()
30 || q{};
31  
32 1 return $self->{executable};
33 }
34  
35 sub library {
36 1 ▶ my ($self) = @_;
Subroutine library: called CC=4 SCAR=15.6
Pod undocumented
37 1 ▶ return $self->{library} if defined $self->{library};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"library"} 0 1 1
38  
39 $self->{library} = $ENV{INLINE_CLIPS_LIB}
40 1 ▶ || _alien_library()
Condition40%2/5
$ENV{'INLINE_CLIPS_LIB'} || _alien_library() || ''
l!l
01
$ENV{"INLINE_CLIPS_LIB"} or _alien_library()
l!l&&r!l&&!r
001
MC/DC0%0/2
$ENV{'INLINE_CLIPS_LIB'} || _alien_library() || ''
$ENV{"INLINE_CLIPS_LIB"} _alien_library()
Truth table33%1/3
A or B
A= $ENV{"INLINE_CLIPS_LIB"}B= _alien_library()
ABresult
1X1
011
000
41 || q{};
42  
43 1 return $self->{library};
44 }
45  
46 sub run_program {
47 0 ▶ my ($self, $program, @commands) = @_;
Subroutine run_program: not called CC=4 SCAR=30.0
Pod undocumented
48 0 ▶ croak 'program text is required' if !defined $program;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $program 0 0 0
49  
50 0 my ($fh, $tmp) = tempfile('inline-clips-XXXX', SUFFIX => '.clp', UNLINK => 1);
51 0 print {$fh} "(clear)\n";
52 0 print {$fh} $program;
53 0 ▶ print {$fh} "\n" if $program !~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $program =~ /\n\z/ 0 0 0
54 0 print {$fh} "(reset)\n";
55 0 print {$fh} "$_\n" for @commands;
56 0 print {$fh} "(exit)\n";
57 0 close $fh;
58  
59 0 return $self->run_file($tmp);
60 }
61  
62 sub run_file {
63 1 ▶ my ($self, $file) = @_;
Subroutine run_file: called CC=7 SCAR=34.0
Pod undocumented
64 1 ▶ croak 'file path is required' if !defined $file || $file eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 0 1 1
Condition33%1/3
not defined $file or $file eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table33%1/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
65 1 ▶ croak "CLIPS file not found: $file" if !-f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 0 1
66  
67 0 my $exe = $self->executable;
68 0 ▶ croak 'CLIPS executable is not available; set INLINE_CLIPS_EXECUTABLE or install CLIPS/Alien::CLIPS'
Branch: 1 branch
BranchTrueFalseTotal
unless $exe 0 0 0
69 if !$exe;
70  
71 0 my $err = gensym;
72 0 my $pid = open3(my $in, my $out, $err, $exe, '-f2', $file);
73 0 close $in;
74  
75 0 local $/ = undef;
76 0 ▶ my $stdout = <$out> // q{};
Condition0%0/2
readline $out // ""
l!l
00
MC/DC0%0/1
readline $out // ""
readline $out
Truth table0%0/2
A
A= readline $out
Aresult
11
00
77 0 ▶ my $stderr = <$err> // q{};
Condition0%0/2
readline $err // ""
l!l
00
MC/DC0%0/1
readline $err // ""
readline $err
Truth table0%0/2
A
A= readline $err
Aresult
11
00
78 0 waitpid($pid, 0);
79 0 my $status = $? >> 8;
80  
81 return {
82 0 status => $status,
83 stdout => $stdout,
84 stderr => $stderr,
85 };
86 }
87  
88 sub _path_executable {
89 0 ▶ my ($name) = @_;
Subroutine _path_executable: not called CC=3 SCAR=24.8
90 0 for my $dir (File::Spec->path) {
91 0 my $candidate = File::Spec->catfile($dir, $name);
92 0 ▶ return $candidate if -x $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if -x $candidate 0 0 0
93 }
94 0 return;
95 }
96  
97 sub _alien_executable {
98 0 ▶ my $alien = _load_alien() or return;
Subroutine _alien_executable: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
my $alien = _load_alien() or return 0 0 0
99 0 ▶ my @bins = $alien->can('bin_dir') ? $alien->bin_dir : ();
Branch: 1 branch
BranchTrueFalseTotal
$alien->can('bin_dir') ? : 0 0 0
100 0 for my $dir (@bins) {
101 0 my $candidate = File::Spec->catfile($dir, 'clips');
102 0 ▶ return $candidate if -x $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if -x $candidate 0 0 0
103 }
104 0 return;
105 }
106  
107 sub _alien_library {
108 1 ▶ my $alien = _load_alien() or return;
Subroutine _alien_library: called CC=4 SCAR=25.0
Branch: 1 branch
BranchTrueFalseTotal
my $alien = _load_alien() or return 1 0 1
109 0 ▶ my @libs = $alien->can('dynamic_libs') ? $alien->dynamic_libs : ();
Branch: 1 branch
BranchTrueFalseTotal
$alien->can('dynamic_libs') ? : 0 0 0
110 0 ▶ return $libs[0] if @libs;
Branch: 1 branch
BranchTrueFalseTotal
if @libs 0 0 0
111 0 return;
112 }
113  
114 sub _load_alien {
115 1 ▶ my $ok = eval {
Subroutine _load_alien: called CC=2 SCAR=7.9
116 1 require Alien::CLIPS;
117 1 Alien::CLIPS->import();
118 0 1;
119 };
120 1 ▶ return if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 0 1
121 0 return 'Alien::CLIPS';
122 }
123  
124 2 1;
125  
126 __END__
↑ Summary blib/lib/Inline/CLIPS.pm »