| 1 | package MyApp; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.40; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use PAGI::Nano; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | # The "grows up" run shape: a modulino. Loading the file returns the app (the | ||||||||||||||||||||||||||||||
| 9 | # last expression), so it runs directly: | ||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||
| 11 | # pagi-server -Ilib lib/MyApp.pm | ||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||
| 13 | # It is also dual-use: `use MyApp; my $app = MyApp->to_app` for tests, or | ||||||||||||||||||||||||||||||
| 14 | # `mount '/tasks' => MyApp->to_app` to nest it inside a larger app. Same app | ||||||||||||||||||||||||||||||
| 15 | # value either way — no rewrite from the single-file form. | ||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | # The handlers initialize their slice of shared state defensively (//= []). This | ||||||||||||||||||||||||||||||
| 18 | # keeps the modulino robust in both run shapes: standalone, startup seeds the | ||||||||||||||||||||||||||||||
| 19 | # state; mounted inside a larger app, the router does not forward lifespan events | ||||||||||||||||||||||||||||||
| 20 | # to mounted children, so the outer app owns lifecycle and the defensive init | ||||||||||||||||||||||||||||||
| 21 | # covers the gap. | ||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub to_app ($class) { | ||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.3
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | return app { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | startup async sub ($state) { $state->{tasks} //= [] }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Condition50%1/2
$state->{"tasks"} //= []
MC/DC0%0/1
$state->{"tasks"} //= []
$state->{"tasks"}
Truth table50%1/2
A
A= $state->{"tasks"}
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | get '/' => sub ($c) { $c->state->{tasks} //= [] }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$c->state->{"tasks"} //= []
MC/DC100%1/1
$c->state->{"tasks"} //= []
$c->state->{"tasks"}
Truth table100%2/2
A
A= $c->state->{"tasks"}
| |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | get '/:id' => sub ($c, $id) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | ($c->state->{tasks} //= [])->[$id - 1] | ||||||||||||||||||||||||||||
|
Condition0%0/5
($c->state->{"tasks"} //= [])->[$id - 1] // $c->json({"error", "not found"}, "status", 404)
$c->state->{"tasks"} //= []
MC/DC0%0/2
($c->state->{"tasks"} //= [])->[$id - 1] // $c->json({"error", "not found"}, "status", 404)
($c->state->{"tasks"} //= [])->[$id - 1]
$c->json({"error", "not found"}, "status", 404)
MC/DC0%0/1
$c->state->{"tasks"} //= []
$c->state->{"tasks"}
Truth table0%0/3
A // B
A= ($c->state->{"tasks"} //= [])->[$id - 1]B= $c->json({"error", "not found"}, "status", 404)
Truth table0%0/2
A
A= $c->state->{"tasks"}
| |||||||||||||||||||||||||||||||
| 30 | // $c->json({ error => 'not found' }, status => 404); | ||||||||||||||||||||||||||||||
| 31 | 3 | }; | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | post '/' => async sub ($c) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||
| 34 | my $attrs = await $c->params->required( | ||||||||||||||||||||||||||||||
| 35 | 'title', +{ tags => [] }, | ||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub ($c, $missing) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 37 | 0 | $c->json({ error => 'missing', fields => $missing }, status => 400); | |||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||||
| 39 | 1 | ); | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my $tasks = $c->state->{tasks} //= []; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$c->state->{"tasks"} //= []
MC/DC0%0/1
$c->state->{"tasks"} //= []
$c->state->{"tasks"}
Truth table50%1/2
A
A= $c->state->{"tasks"}
| |||||||||||||||||||||||||||||||
| 41 | 1 | push @$tasks, { id => scalar(@$tasks) + 1, %$attrs }; | |||||||||||||||||||||||||||||
| 42 | 1 | $c->json($tasks->[-1], status => 201); | |||||||||||||||||||||||||||||
| 43 | 3 | }; | |||||||||||||||||||||||||||||
| 44 | 3 | }; | |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 1 | __PACKAGE__->to_app; | |||||||||||||||||||||||||||||