| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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 | |||||||||||