blib/lib/Langertha/Raider/PerlTools.pm

filter: Statementstmt 98.9% 190 / 192 Branchbran 86.8% 66 / 76 Conditioncond 73.3% 44 / 60 MC/DCmcdc 53.1% 17 / 32 Subroutinesub 96.4% 27 / 28 Podpod 100% 1 / 1 totaltotal 88.6% 345 / 389 CC 66 SCAR 23.0
CC
66
Cov
91%
__ANON__
22.4
__ANON__
16.3
__ANON__
7.0
CRAP 10.0
« blib/lib/Langertha/Raider/Hall/ACP.pm ↑ Summary blib/lib/Langertha/Raider/WebTools.pm »
1 package Langertha::Raider::PerlTools;
2 76 our $VERSION = '0.503';
3 # ABSTRACT: MCP::Server factory with Perl evaluation, syntax check, and module install
4  
5 76 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 76 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 76 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 76 ▶use File::Which ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 76 ▶use MCP::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 76 ▶use IPC::Run qw( run start timeout );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 76 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 76 ▶use Langertha::Raider::Binary qw( packed_binary );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 76 ▶use Langertha::Raider::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 76 ▶use Langertha::Raider::SessionStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 76 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 76 our @EXPORT_OK = qw( build_perl_tools_server );
18  
19  
20 # The perl the tools run, or undef when there is none. A raider script
21 # runs them with its own perl. The standalone binary (PAR::Packer) has
22 # none: $^X is not a usable interpreter there, so it takes the perl on PATH.
23 31 ▶sub _tool_perl {
Subroutine _tool_perl: called CC=2 SCAR=6.9
24 31 ▶ return $^X unless packed_binary();
Branch: 1 branch
BranchTrueFalseTotal
unless packed_binary() 25 6 31
25 6 return scalar File::Which::which('perl');
26 }
27  
28 # The $] of $perl, for the lib's perl-version marker: the perl that uses
29 # the lib, which in the standalone binary is not the one running this
30 # code. undef when that perl does not answer.
31 8 ▶sub _tool_perl_version {
Subroutine _tool_perl_version: called CC=4 SCAR=14.3
32 8 my ( $perl ) = @_;
33 8 ▶ return $] unless packed_binary();
Branch: 1 branch
BranchTrueFalseTotal
unless packed_binary() 7 1 8
34 1 my ( $out, $err ) = ( '', '' );
35 1 ▶ return unless eval { run([ $perl, '-e', 'print $]' ], \'', \$out, \$err, timeout(30)) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { run([$perl, "-e", "print \$]"], \"", \$out, \$err, timeout(30)) } } 0 1 1
36 1 ▶ return $1 if $out =~ /\A(\d+\.\d+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $out =~ /\A(\d+\.\d+)\z/ 1 0 1
37 0 return;
38 }
39  
40 3 ▶sub _no_perl_message {
Subroutine _no_perl_message: called CC=2 SCAR=6.9
41 3 my ( $tool_name, $what ) = @_;
42 3 ▶ return $tool_name.': no perl to run '.( $what // 'the code' ).': this raider is the standalone '
Condition100%2/2
$what // "the code"
l!l
12
MC/DC100%1/1
$what // "the code"
$what
Truth table100%2/2
A
A= $what
Aresult
11
00
43 . 'binary, which has no perl interpreter of its own, and there is no perl '
44 . 'on PATH. Install perl or put it on PATH.';
45 }
46  
47 59 ▶sub build_perl_tools_server {
Subroutine build_perl_tools_server: called CC=3 SCAR=11.0
Pod documented
48 59 my %args = @_;
49 59 ▶ my $root = path($args{root} // '.')->absolute;
Condition50%1/2
$args{"root"} // "."
l!l
590
MC/DC0%0/1
$args{"root"} // "."
$args{"root"}
Truth table50%1/2
A
A= $args{"root"}
Aresult
11
00
50 59 my $lib_target_override = $args{lib_target};
51 59 ▶ my $install_timeout = $args{install_timeout} // 300;
Condition100%2/2
$args{"install_timeout"} // 300
l!l
158
MC/DC100%1/1
$args{"install_timeout"} // 300
$args{"install_timeout"}
Truth table100%2/2
A
A= $args{"install_timeout"}
Aresult
11
00
52  
53 25 ▶ my $resolve_lib_target = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
54 25 ▶ return path($lib_target_override)->absolute($root)->stringify if defined $lib_target_override;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lib_target_override 3 22 25
55 22 return Langertha::Raider::Home->project_base($root)->child('lib')->stringify;
56 59 };
57  
58 19 ▶ my $perl5lib_for = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
59 19 my ($target) = @_;
60 19 my $base = path($target)->child('lib', 'perl5')->stringify;
61 19 ▶ return join ':', grep { defined && length } ($base, $ENV{PERL5LIB});
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
62 59 };
63  
64 10 ▶ my $in_root = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
65 10 my ($p) = @_;
66 10 ▶ return 0 if grep { $_ eq '..' } split m{/}, $p;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "..";} split(m[/], $p, 0) 2 8 10
67 8 return $root->subsumes(path($p)->absolute($root));
68 59 };
69  
70 59 ▶ my $chdir_root = sub { chdir $root or die "chdir $root: $!\n" };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
chdir $root or die "chdir ${root}: $!\n" 0 0 0
71  
72 # IPC::Run leaves the child running when finish dies on a timeout:
73 # kill it (TERM, then KILL after the grace period) before rethrowing.
74 30 ▶ my $finish = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
75 30 my ($h) = @_;
76 30 ▶ unless (eval { $h->finish; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $h->finish; 1 } }) 5 25 30
77 5 my $e = $@;
78 5 eval { $h->kill_kill(grace => 2) };
79 5 die $e;
80 }
81 25 my $fr = $h->full_result;
82 25 ▶ return defined($fr) ? ($fr >> 8) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $fr ? : 25 0 25
83 59 };
84  
85 # Runs cpanm; a timeout or start failure comes back as the second
86 # value ('timeout' or the message) instead of as an exception.
87 12 ▶ my $run_cpanm = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
88 12 my ($cmd, $out, $err) = @_;
89 12 my $empty = '';
90 12 my $rc;
91 12 ▶ return ($rc) if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $rc = &$finish(start($cmd, \$empty, $out, $err, timeout($install_timeout))); 1 } } 10 2 12
92 12 $rc = $finish->(start $cmd, \$empty, $out, $err, timeout($install_timeout));
93 10 1;
94 };
95 2 ▶ my $failure = $@ =~ /^IPC::Run: timeout on timer/ ? 'timeout' : $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /^IPC::Run: timeout on timer/ ? : 2 0 2
96 2 chomp $failure;
97 2 return (-1, $failure);
98 59 };
99  
100 # $perl is the perl that will use the lib; the marker records its version.
101 12 ▶ my $ensure_lib_init = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
102 12 my ($target, $perl) = @_;
103 12 my $dir = path($target);
104 12 ▶ return if -d $dir && -f $dir->child('cpanfile');
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir and -f $dir->child("cpanfile") 4 8 12
Condition100%3/3
-d $dir and -f $dir->child("cpanfile")
!ll&&!rl&&r
714
MC/DC100%2/2
-d $dir and -f $dir->child("cpanfile")
-d $dir -f $dir->child("cpanfile")
Truth table100%3/3
A and B
A= -d $dirB= -f $dir->child("cpanfile")
ABresult
0X0
100
111
105  
106 # A target in the project's .raider/ comes with its .gitignore.
107 8 my $store = Langertha::Raider::SessionStore->new(root => $root->stringify);
108 8 ▶ $store->prepare_base if $store->base->subsumes($dir->absolute($root));
Branch: 1 branch
BranchTrueFalseTotal
if $store->base->subsumes($dir->absolute($root)) 7 1 8
109 8 $dir->mkpath;
110 8 my $perl_version = _tool_perl_version($perl);
111 8 ▶ $dir->child('perl-version')->spew_utf8($perl_version."\n") if defined $perl_version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perl_version 8 0 8
112 8 $dir->child('cpanfile')->spew_utf8(";\n");
113 8 return;
114 59 };
115  
116 9 ▶ my $append_to_cpanfile = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
117 9 my ($target, $module) = @_;
118 9 my $cpanfile = path($target)->child('cpanfile');
119 9 my $content = $cpanfile->slurp_utf8;
120 9 ▶ return if $content =~ /\b\Q$module\E\b/m;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\b\Q$module\E\b/m 1 8 9
121 8 $cpanfile->append_utf8("requires '$module';\n");
122 8 return;
123 59 };
124  
125 # --- Tool: perl_eval ---
126  
127 59 my $server = MCP::Server->new(name => 'raider-perl', version => '1.0');
128  
129 $server->tool(
130 name => 'perl_eval',
131 description => 'Evaluate a snippet of Perl code and return stdout, stderr, exit code, and return value. Runs in the working root with the private lib on PERL5LIB. No persistent session — each call starts fresh. On missing-module error: auto-installs once and retries.',
132 input_schema => {
133 type => 'object',
134 properties => {
135 code => { type => 'string', description => 'Perl code to eval (as one-liner or block)' },
136 stdin => { type => 'string', description => 'String to feed to STDIN (optional)' },
137 timeout => { type => 'integer', description => 'Max seconds before kill (default: 60)', default => 60 },
138 },
139 required => ['code'],
140 },
141 15 ▶ code => sub {
Subroutine __ANON__: called CC=22 SCAR=31.1
142 15 my ($tool, $in) = @_;
143 15 ▶ my $code = $in->{code} // '';
Condition50%1/2
$$in{"code"} // ""
l!l
150
MC/DC0%0/1
$$in{"code"} // ""
$$in{"code"}
Truth table50%1/2
A
A= $$in{"code"}
Aresult
11
00
144 15 ▶ my $stdin = $in->{stdin} // '';
Condition100%2/2
$$in{"stdin"} // ""
l!l
114
MC/DC100%1/1
$$in{"stdin"} // ""
$$in{"stdin"}
Truth table100%2/2
A
A= $$in{"stdin"}
Aresult
11
00
145 15 ▶ my $to_sec = $in->{timeout} // 60;
Condition100%2/2
$$in{"timeout"} // 60
l!l
69
MC/DC100%1/1
$$in{"timeout"} // 60
$$in{"timeout"}
Truth table100%2/2
A
A= $$in{"timeout"}
Aresult
11
00
146  
147 15 my $perl = _tool_perl();
148 15 ▶ return $tool->text_result(_no_perl_message('perl_eval'), 1) unless defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perl 1 14 15
149  
150 14 my ($out, $err, $auto_installed, $failure);
151  
152 15 ▶ my $do_run = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 15 my @cmd = ($perl, '-e', $code);
154 15 my $target = $resolve_lib_target->();
155 15 local $ENV{PERL5LIB} = $perl5lib_for->($target);
156 15 my $h = start \@cmd, \$stdin, \$out, \$err, init => $chdir_root, timeout($to_sec);
157 14 return $finish->($h);
158 14 };
159  
160 # Both the first run and the retry after auto-install report a
161 # timeout or start failure as error, never as an exception.
162 15 ▶ my $guarded_run = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
163 15 my $rc;
164 15 ▶ return $rc if eval { $rc = $do_run->(); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $rc = &$do_run(); 1 } } 11 4 15
165 4 ▶ $failure = $@ =~ /^IPC::Run: timeout on timer/ ? 'timeout' : $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /^IPC::Run: timeout on timer/ ? : 3 1 4
166 4 chomp $failure;
167 4 ▶ $err //= '';
Condition100%2/2
$err //= ""
l!l
31
MC/DC100%1/1
$err //= ""
$err
Truth table100%2/2
A
A= $err
Aresult
11
00
168 4 chomp $err;
169 4 return -1;
170 14 };
171  
172 14 my $rc = $guarded_run->();
173  
174 # Auto-recover: check for "Can't locate X/Y.pm" once
175 14 my @missing;
176 14 ▶ if ($err && $err =~ /^Can't locate (\S+\.pm)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and $err =~ /^Can't locate (\S+\.pm)/m) 2 12 14
Condition100%3/3
$err and $err =~ /^Can't locate (\S+\.pm)/m
!ll&&!rl&&r
1022
MC/DC100%2/2
$err and $err =~ /^Can't locate (\S+\.pm)/m
$err $err =~ /^Can't locate (\S+\.pm)/m
Truth table100%3/3
A and B
A= $errB= $err =~ /^Can't locate (\S+\.pm)/m
ABresult
0X0
100
111
177 2 my $mod = $1;
178 2 $mod =~ s{/}{::}g;
179 2 $mod =~ s{\.pm$}{};
180 2 push @missing, $mod;
181 }
182  
183 14 ▶ if (@missing && !$auto_installed && !$failure) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing and not $auto_installed and not $failure) 2 12 14
Condition66%4/6
@missing and not $auto_installed and not $failure
!ll&&!rl&&r
1202
@missing and not $auto_installed
!ll&&!rl&&r
1202
MC/DC33%1/3
@missing and not $auto_installed and not $failure
@missing not $auto_installed not $failure
Truth table50%2/4
A and not(B) and not(C)
A= @missingB= not $auto_installedC= not $failure
ABCresult
0XX0
10X0
1100
1111
184 2 my ($i_out, $i_err);
185 2 my $target = $resolve_lib_target->();
186 2 $ensure_lib_init->($target, $perl);
187 2 my ($i_rc, $i_failure) = $run_cpanm->(['cpanm', '--local-lib', $target, @missing], \$i_out, \$i_err);
188  
189 2 ▶ if ($i_rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_rc == 0) { } 1 1 2
190 1 $auto_installed = \@missing;
191 1 $append_to_cpanfile->($target, $_) for @missing;
192 1 $rc = $guarded_run->();
193 }
194 else {
195 1 ▶ $err .= "\n[auto-install failed for @missing: ".($i_failure // $i_err // '')."]";
Condition40%2/5
($i_failure // $i_err) // ""
l!l
10
$i_failure // $i_err
l!l&&r!l&&!r
100
MC/DC0%0/2
($i_failure // $i_err) // ""
$i_failure $i_err
Truth table33%1/3
A // B
A= $i_failureB= $i_err
ABresult
1X1
011
000
196 }
197 }
198  
199 14 my $return_value;
200 14 ▶ if (defined $out && length $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out and length $out) 6 8 14
Condition100%3/3
defined $out and length $out
!ll&&!rl&&r
176
MC/DC100%2/2
defined $out and length $out
defined $out length $out
Truth table100%3/3
A and B
A= defined $outB= length $out
ABresult
0X0
100
111
201 6 chomp(my @lines = split /\n/, $out);
202 6 ▶ $return_value = $lines[-1] if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 6 0 6
203 }
204  
205 14 ▶ my %result = (
Condition66%4/6
$out // ""
l!l
131
$err // ""
l!l
140
$rc // 0
l!l
140
MC/DC100%1/1
$out // ""
$out
MC/DC0%0/1
$err // ""
$err
MC/DC0%0/1
$rc // 0
$rc
Truth table100%2/2
A
A= $out
Aresult
11
00
Truth table50%1/2
A
A= $err
Aresult
11
00
Truth table50%1/2
A
A= $rc
Aresult
11
00
206 stdout => $out // '',
207 stderr => $err // '',
208 return_value => $return_value,
209 exit_code => $rc // 0,
210 );
211 14 ▶ $result{auto_installed} = $auto_installed if $auto_installed;
Branch: 1 branch
BranchTrueFalseTotal
if $auto_installed 1 13 14
212 14 ▶ $result{error} = $failure if $failure;
Branch: 1 branch
BranchTrueFalseTotal
if $failure 4 10 14
213  
214 14 return $tool->structured_result(\%result);
215 },
216 59 );
217  
218 # --- Tool: perl_check ---
219  
220 $server->tool(
221 name => 'perl_check',
222 description => 'Compile-check Perl code with "perl -c" in the working root, with the private lib on PERL5LIB. Not a sandbox: BEGIN blocks and use statements DO run during compilation.',
223 input_schema => {
224 type => 'object',
225 properties => {
226 code => { type => 'string', description => 'Perl code to syntax-check' },
227 },
228 required => ['code'],
229 },
230 5 ▶ code => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
231 5 my ($tool, $in) = @_;
232 5 ▶ my $code = $in->{code} // '';
Condition50%1/2
$$in{"code"} // ""
l!l
50
MC/DC0%0/1
$$in{"code"} // ""
$$in{"code"}
Truth table50%1/2
A
A= $$in{"code"}
Aresult
11
00
233  
234 5 my $perl = _tool_perl();
235 5 ▶ return $tool->text_result(_no_perl_message('perl_check'), 1) unless defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perl 1 4 5
236  
237 4 my ($out, $err);
238 4 local $ENV{PERL5LIB} = $perl5lib_for->($resolve_lib_target->());
239 4 my $h = start [$perl, '-c', '-'], \$code, \$out, \$err, init => $chdir_root, timeout(30);
240 4 my $rc = $finish->($h);
241  
242 4 ▶ my $valid = ($rc == 0) ? JSON::MaybeXS::true() : JSON::MaybeXS::false();
Branch: 1 branch
BranchTrueFalseTotal
$rc == 0 ? : 3 1 4
243 4 my $syntax_error;
244 4 ▶ if ($rc != 0 && $err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0 and $err) 1 3 4
Condition66%2/3
$rc != 0 and $err
!ll&&!rl&&r
301
MC/DC50%1/2
$rc != 0 and $err
$rc != 0 $err
Truth table66%2/3
A and B
A= $rc != 0B= $err
ABresult
0X0
100
111
245 1 $syntax_error = $err;
246 1 chomp $syntax_error;
247 }
248  
249 4 ▶ return $tool->structured_result({
Condition100%2/2
$syntax_error // undef
l!l
13
MC/DC100%1/1
$syntax_error // undef
$syntax_error
Truth table100%2/2
A
A= $syntax_error
Aresult
11
00
250 valid => $valid,
251 syntax_error => $syntax_error // undef,
252 });
253 },
254 59 );
255  
256 # --- Tool: perl_cpanm ---
257  
258 $server->tool(
259 name => 'perl_cpanm',
260 description => 'Install a CPAN module into a private local::lib. Target: options.target (must lie inside the working root) > configured lib target > .raider/lib in the working root. Creates the target directory (with cpanfile + perl-version marker) on first install.',
261 input_schema => {
262 type => 'object',
263 properties => {
264 module => { type => 'string', description => 'Module name (or distribution) to install' },
265 options => {
266 type => 'object',
267 description => 'Installation options',
268 properties => {
269 test => { type => 'boolean', description => 'Run tests before install (default: false)' },
270 force => { type => 'boolean', description => 'Force install (ignore errors)' },
271 from => { type => 'string', description => 'CPAN mirror URL or path' },
272 target => { type => 'string', description => 'Override lib target directory (inside the working root; relative paths resolve against it)' },
273 },
274 },
275 },
276 required => ['module'],
277 },
278 14 ▶ code => sub {
Subroutine __ANON__: called CC=16 SCAR=27.9
279 14 my ($tool, $in) = @_;
280 14 ▶ my $module = $in->{module} // '';
Condition50%1/2
$$in{"module"} // ""
l!l
140
MC/DC0%0/1
$$in{"module"} // ""
$$in{"module"}
Truth table50%1/2
A
A= $$in{"module"}
Aresult
11
00
281 14 ▶ my $opts = $in->{options} // {};
Condition100%2/2
$$in{"options"} // {}
l!l
104
MC/DC100%1/1
$$in{"options"} // {}
$$in{"options"}
Truth table100%2/2
A
A= $$in{"options"}
Aresult
11
00
282 14 ▶ my $explicit_target = $opts->{target} // undef;
Condition100%2/2
$$opts{"target"} // undef
l!l
104
MC/DC100%1/1
$$opts{"target"} // undef
$$opts{"target"}
Truth table100%2/2
A
A= $$opts{"target"}
Aresult
11
00
283  
284 14 ▶ if (defined $explicit_target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explicit_target) 10 4 14
285 10 ▶ return $tool->text_result('perl_cpanm: target '.$explicit_target.' is outside the working root '.$root, 1)
Branch: 1 branch
BranchTrueFalseTotal
unless &$in_root($explicit_target) 3 7 10
286 unless $in_root->($explicit_target);
287 }
288  
289 # cpanm is a perl script, and the modules are for the tool perl.
290 11 my $perl = _tool_perl();
291 11 ▶ return $tool->text_result(_no_perl_message('perl_cpanm', 'cpanm'), 1) unless defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perl 1 10 11
292  
293 10 ▶ my $target_dir = defined $explicit_target
Branch: 1 branch
BranchTrueFalseTotal
defined $explicit_target ? : 6 4 10
294 ? path($explicit_target)->absolute($root)->stringify
295 : $resolve_lib_target->();
296  
297 10 $ensure_lib_init->($target_dir, $perl);
298  
299 10 my @cmd = ('cpanm', '--local-lib', $target_dir);
300 10 ▶ push @cmd, '--test' if $opts->{test};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"test"} 0 10 10
301 10 ▶ push @cmd, '--force' if $opts->{force};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"force"} 0 10 10
302 10 ▶ push @cmd, '--from', $opts->{from} if $opts->{from};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"from"} 1 9 10
303 10 push @cmd, $module;
304  
305 10 my ($out, $err);
306 10 my ($rc, $failure) = $run_cpanm->(\@cmd, \$out, \$err);
307  
308 10 ▶ $append_to_cpanfile->($target_dir, $module) if $rc == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 0 8 2 10
309  
310 10 ▶ my $installed = ($rc == 0) ? JSON::MaybeXS::true() : JSON::MaybeXS::false();
Branch: 1 branch
BranchTrueFalseTotal
$rc == 0 ? : 8 2 10
311  
312 10 ▶ return $tool->structured_result({
Branch: 1 branch
BranchTrueFalseTotal
$failure ? : 1 9 10
Condition50%2/4
$out // ""
l!l
100
$err // ""
l!l
100
MC/DC0%0/1
$out // ""
$out
MC/DC0%0/1
$err // ""
$err
Truth table50%1/2
A
A= $out
Aresult
11
00
Truth table50%1/2
A
A= $err
Aresult
11
00
313 installed => $installed,
314 target => $target_dir,
315 version => undef,
316 stdout => $out // '',
317 stderr => $err // '',
318 $failure ? ( error => $failure ) : (),
319 });
320 },
321 59 );
322  
323 59 return $server;
324 }
325  
326 76 1;
327  
328 __END__
329  
330 =pod
331  
332 =encoding UTF-8
333  
334 =head1 NAME
335  
336 Langertha::Raider::PerlTools - MCP::Server factory with Perl evaluation, syntax check, and module install
337  
338 =head1 VERSION
339  
340 version 0.503
341  
342 =head1 DESCRIPTION
343  
344 The Perl-native tools of L<raider>: an L<MCP::Server> with
345 C<perl_eval(code, [stdin], [timeout])>, C<perl_check(code)> and
346 C<perl_cpanm(module, [options])>, all run as subprocesses in the working
347 root, with a private local::lib on C<PERL5LIB>. C<perl_eval> and
348 C<perl_check> run the perl that runs raider; the standalone binary has no
349 usable perl of its own, so there they run the C<perl> on C<PATH>. Without
350 one all three tools are a tool error there, and nothing runs.
351  
352 =head2 build_perl_tools_server
353  
354 my $server = Langertha::Raider::PerlTools::build_perl_tools_server(
355 root => '/some/dir', # working root (default: the current directory)
356 lib_target => '.raider/lib', # optional override
357 install_timeout => 300, # seconds per cpanm run (default 300)
358 );
359  
360 Returns an L<MCP::Server> instance with the tools C<perl_eval>, C<perl_check>,
361 and C<perl_cpanm> registered. A lib target inside the project's F<.raider/>
362 (the default, F<.raider/lib>) is created through
363 L<Langertha::Raider::SessionStore/prepare_base>, which writes
364 F<.raider/.gitignore> when there is none.
365  
366 =head1 TOOLS
367  
368 =head2 perl_eval
369  
370 Evaluate Perl code with the running perl (C<$^X>) in the working root,
371 with the private lib on C<PERL5LIB>, killed after C<timeout> seconds
372 (default 60). Returns stdout, stderr, exit_code,
373 return_value, and (optionally) auto_installed. C<error> is C<timeout> when
374 the time limit hit, otherwise the message of whatever else failed.
375  
376 On "Can't locate X/Y.pm" in stderr, auto-installs the module once then
377 retries the eval. If retry still fails, returns the error. A failed or
378 timed-out install is noted in C<stderr>.
379  
380 In the standalone binary, which has no usable perl of its own, the code
381 runs with the C<perl> on C<PATH>. Without one the call is a tool error
382 that says so, and nothing runs.
383  
384 =head2 perl_check
385  
386 Compile-check Perl code via C<perl -c> in the working root, with the
387 private lib on C<PERL5LIB>. This is not a sandbox: C<BEGIN> blocks and
388 C<use> statements run during compilation, for at most 30 seconds. The perl
389 is the one L</perl_eval> uses.
390 Returns C<valid> (bool) and
391 C<syntax_error> (string or null).
392  
393 =head2 perl_cpanm
394  
395 Install a CPAN module into a private local::lib. An explicit
396 C<options.target> must lie inside the working root (relative paths resolve
397 against it); anything else is rejected. Creates the target
398 directory (with cpanfile + perl-version marker) on first install. The
399 marker holds C<$]> of the perl the lib is for, the one L</perl_eval> uses;
400 in the standalone binary that perl is asked for it. The
401 cpanfile is updated idempotently (no duplicate entries).
402 In the standalone binary without a C<perl> on C<PATH> the call is a tool
403 error, as for L</perl_eval>, and cpanm does not run.
404 C<error> is C<timeout> when cpanm hit the C<install_timeout> limit,
405 otherwise the message of whatever else kept it from running.
406  
407 =head1 SUPPORT
408  
409 =head2 Issues
410  
411 Please report bugs and feature requests on GitHub at
412 L<https://github.com/Getty/langertha-raider/issues>.
413  
414 =head2 IRC
415  
416 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
417  
418 =head1 CONTRIBUTING
419  
420 Contributions are welcome! Please fork the repository and submit a pull request.
421  
422 =head1 AUTHOR
423  
424 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
425  
426 =head1 COPYRIGHT AND LICENSE
427  
428 This software is copyright (c) 2026 by Torsten Raudssus.
429  
430 This is free software; you can redistribute it and/or modify it under
431 the same terms as the Perl 5 programming language system itself.
432  
433 =cut
« blib/lib/Langertha/Raider/Hall/ACP.pm ↑ Summary blib/lib/Langertha/Raider/WebTools.pm »