Agent/Hook.pm

filter: Statementstmt 28.0% 7 / 25 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 2 / 4 Podpod 0.0% 0 / 2 totaltotal 23.0% 9 / 39 CC 5 SCAR 24.6
CC
5
Cov
21%
load
20.0
invoke
6.0
BEGIN
1.0
CRAP 11.8
« Agent/Compiler.pm ↑ Summary Agent/Parser.pm »
1 package Sim::Agent::Hook;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 sub load
8 0 ▶{
Subroutine load: not called CC=4 SCAR=30.0
Pod undocumented
9  
10 0 my ($file) = @_;
11  
12 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot open hook file: $file" 0 0 0
13 or die "Cannot open hook file: $file";
14  
15 0 local $/;
16 0 my $code = <$fh>;
17 0 close $fh;
18  
19 0 my $pkg = "Sim::Agent::Hook::Run::" . int(rand(1_000_000));
20  
21 0 my $wrapped = "package $pkg;\n$code";
22  
23 0 my $sub = eval $wrapped;
24  
25 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
26 {
27 0 die "Hook compile error in $file:\n$@";
28 }
29  
30 0 ▶ die "Hook file $file did not return coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 0 0
31 unless ref($sub) eq 'CODE';
32  
33 0 return $sub;
34 }
35  
36  
37 sub invoke
38 0 ▶{
Subroutine invoke: not called CC=2 SCAR=17.9
Pod undocumented
39  
40 0 my ($coderef, @args) = @_;
41  
42 my $result = eval
43 0 {
44 0 $coderef->(@args);
45 };
46  
47 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
48 {
49 0 die "Hook runtime error:\n$@";
50 }
51  
52 0 return $result;
53 }
54  
55  
56  
57  
58  
59 1 1;
60  
61 #my $hook = Sim::Agent::Hook::load($file);
62 #my $result = Sim::Agent::Hook::invoke($hook, $context, $agent, $runner);
63  
64  
65 =pod
66  
67 =head1 NAME
68  
69 Sim::Agent::Hook - Loads and invokes hook files that return coderefs
70  
71 =head1 DESCRIPTION
72  
73 Loads a Perl file, evaluates it in an isolated package, verifies it returns a coderef, and invokes it with structured context.
74  
75 See L<Sim::Agent> for hook contracts.
76  
77 =head1 AUTHOR
78  
79 Gian Luca Brunetti (2026), gianluca.brunetti@gmail.com
80  
81 =head1 LICENSE
82  
83 The GNU General Public License v3.0
84  
85 =cut
86  
87  
« Agent/Compiler.pm ↑ Summary Agent/Parser.pm »