File Coverage

blib/lib/Sim/Agent/Hook.pm
Criterion Covered Total %
statement 6 24 25.0
branch 0 8 0.0
condition n/a
subroutine 2 4 50.0
pod 0 2 0.0
total 8 38 21.0


line stmt bran cond sub pod time code
1             package Sim::Agent::Hook;
2              
3 1     1   8 use strict;
  1         2  
  1         41  
4 1     1   5 use warnings;
  1         2  
  1         393  
5              
6              
7             sub load
8             {
9              
10 0     0 0   my ($file) = @_;
11              
12 0 0         open my $fh, '<', $file
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 0         if ($@)
26             {
27 0           die "Hook compile error in $file:\n$@";
28             }
29              
30 0 0         die "Hook file $file did not return coderef"
31             unless ref($sub) eq 'CODE';
32              
33 0           return $sub;
34             }
35              
36              
37             sub invoke
38             {
39              
40 0     0 0   my ($coderef, @args) = @_;
41              
42             my $result = eval
43 0           {
44 0           $coderef->(@args);
45             };
46              
47 0 0         if ($@)
48             {
49 0           die "Hook runtime error:\n$@";
50             }
51              
52 0           return $result;
53             }
54              
55              
56              
57              
58              
59             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 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