| 1 | 366 | ▶ | use v5.36; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | |||||||||||||
| 3 | package Ekorn::Pipeline::Workers::Wire; | ||||||||||||
| 4 | |||||||||||||
| 5 | # What the program and its workers share: messages framed by length on | ||||||||||||
| 6 | # a socket, and functions found by name. For Ekorn::Pipeline::Workers' | ||||||||||||
| 7 | # own modules, not its callers. | ||||||||||||
| 8 | |||||||||||||
| 9 | 366 | ▶ | use Exporter qw(import); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 366 | our $VERSION = 'v0.1.0'; | |||||||||||
| 12 | 366 | our @EXPORT_OK = qw(write_frame read_frame resolve unplaced token_variable); | |||||||||||
| 13 | |||||||||||||
| 14 | |||||||||||||
| 15 | # The environment variable that gives a worker its token: its | ||||||||||||
| 16 | # environment, not its command line, which every user of the computer | ||||||||||||
| 17 | # can read (Ekorn-Pipeline's 2026-0065, D16). | ||||||||||||
| 18 | 372 | ▶ | sub token_variable () { | ||||||||||
|
Subroutine
token_variable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 19 | 372 | return 'EKORN_PIPELINE_WORKERS_TOKEN'; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | |||||||||||||
| 23 | # A message without the places Perl added to it: Storable's own file | ||||||||||||
| 24 | # and line, and the pool's, say nothing a caller can act on. | ||||||||||||
| 25 | 287 | ▶ | sub unplaced ($message) { | ||||||||||
|
Subroutine
unplaced: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 26 | 287 | return $message =~ s( (?: ,?\ at\ \S+\ line\ \d+ )+ \.? \n? \z )()xsr; | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | |||||||||||||
| 30 | # The code a name gives, loading its module if it must; undef if none. | ||||||||||||
| 31 | # A module that won't load dies with Perl's reason, less its place, so | ||||||||||||
| 32 | # a caller learns why, not only that there's no function. | ||||||||||||
| 33 | 4435 | ▶ | sub resolve ($name) { | ||||||||||
|
Subroutine
resolve: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 34 | 4435 | my($module) = $name =~ m( \A (.+) :: \w+ \z )x; | |||||||||||
| 35 | 366 | ▶ | no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | 4435 | ▶ | unless (defined &{ $name }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 579 | (my $file = "$module.pm") =~ s( :: )(/)gx; | |||||||||||
| 38 | 579 | ▶ | eval { require $file; 1 } or die unplaced($@) . "\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | } | ||||||||||||
| 40 | 4216 | ▶ | return defined &{ $name } ? \&{ $name } : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | |||||||||||||
| 44 | # Write a frame: its length, then it. False if the socket's gone. | ||||||||||||
| 45 | 21626 | ▶ | sub write_frame ($fh, $frame) { | ||||||||||
|
Subroutine
write_frame: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 46 | 21626 | my $data = pack('N', length $frame) . $frame; | |||||||||||
| 47 | 21626 | ▶ | while (length $data) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 21626 | my $wrote = syswrite $fh, $data; | |||||||||||
| 49 | 21626 | ▶ | return 0 unless $wrote; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 21626 | substr $data, 0, $wrote, ''; | |||||||||||
| 51 | } | ||||||||||||
| 52 | 21626 | return 1; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | |||||||||||||
| 56 | 53866 | ▶ | sub _read_exactly ($fh, $length) { | ||||||||||
|
Subroutine
_read_exactly: called CC=3 SCAR=11.0
| |||||||||||||
| 57 | 53866 | my $data = ''; | |||||||||||
| 58 | 53866 | ▶ | while (length $data < $length) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 53866 | my $read = sysread $fh, $data, $length - length $data, length $data; | |||||||||||
| 60 | 53866 | ▶ | return undef unless $read; ## no critic (Subroutines::ProhibitExplicitReturnUndef) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | } | ||||||||||||
| 62 | 53626 | return $data; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | |||||||||||||
| 66 | 27053 | ▶ | sub read_frame ($fh) { | ||||||||||
|
Subroutine
read_frame: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 67 | 27053 | ▶ | my $header = _read_exactly($fh, 4) // return undef; ## no critic (Subroutines::ProhibitExplicitReturnUndef) | ||||||||||
|
Condition100%2/2
_read_exactly($fh, 4) // (return undef)
MC/DC100%1/1
_read_exactly($fh, 4) // (return undef)
_read_exactly($fh, 4)
Truth table100%2/2
A
A= _read_exactly($fh, 4)
| |||||||||||||
| 68 | 26813 | return _read_exactly($fh, unpack 'N', $header); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 366 | 1; | |||||||||||
| 72 | |||||||||||||
| 73 | __END__ | ||||||||||||