| 1 | package Plack::ResponseHelper; | ||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | our $HELPERS = {}; | |||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||
| 9 | 6 | shift; | |||||||||
| 10 | 6 | my %options = @_; | |||||||||
| 11 | 6 | foreach my $type (keys %options) { | |||||||||
| 12 | my ($helper, $init) = ref $options{$type} | ||||||||||
| 13 | 2 | ? @{$options{$type}} | |||||||||
| 14 | 5 | ▶ | : ($options{$type}, undef); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | |||||||||||
| 16 | 5 | $helper =~ s/^\+//; | |||||||||
| 17 | 5 | ▶ | croak "bad helper name $helper" unless $helper =~ /^\w+(?:::\w+)*$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | |||||||||||
| 19 | 5 | my $package = "Plack::ResponseHelper::$helper"; | |||||||||
| 20 | 5 | ▶ | eval "require $package" or croak $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 6 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 5 | my $code = *{$package.'::helper'}->($init); | |||||||||
| 24 | 5 | $HELPERS->{$type} = $code; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 6 | my $pkg = caller; | |||||||||
| 28 | 6 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 6 | *{$pkg.'::respond'} = \&respond; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 9 | ▶ | sub respond { | ||||||||
|
Subroutine
respond: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||
| 33 | 9 | my $type = shift; | |||||||||
| 34 | 9 | my $r = shift; | |||||||||
| 35 | |||||||||||
| 36 | 9 | ▶ | croak "unknown type '$type'" unless exists $HELPERS->{$type}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 8 | $r = $HELPERS->{$type}->($r); | |||||||||
| 38 | |||||||||||
| 39 | 7 | ▶ | if (ref $r =~ /^(?:ARRAY|CODE)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | return $r; | |||||||||
| 41 | } | ||||||||||
| 42 | 7 | ▶ | if (UNIVERSAL::isa($r, "Plack::Response")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 7 | return $r->finalize(); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | croak "bad response"; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 6 | 1; | |||||||||
| 50 | __END__ | ||||||||||