| 1 | package Contextual::Return::Failure; | ||||||||||||||
| 2 | 31 | our $VERSION = 0.000_003; | |||||||||||||
| 3 | |||||||||||||||
| 4 | 31 | ▶ | use Contextual::Return; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 31 | ▶ | BEGIN { *_in_context = *Contextual::Return::_in_context } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 31 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 31 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 31 | my %handler_for; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 4 | ▶ | sub _FAIL_WITH { | ||||||||||||
|
Subroutine
_FAIL_WITH: called CC=13 SCAR=25.7
| |||||||||||||||
| 13 | # Unpack and vet args... | ||||||||||||||
| 14 | 4 | my $flag = shift; | |||||||||||||
| 15 | 4 | my $selector_ref; | |||||||||||||
| 16 | 4 | ▶ | if (ref $flag eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | 1 | $selector_ref = $flag; | |||||||||||||
| 18 | 1 | $flag = undef; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | else { | ||||||||||||||
| 21 | 3 | $selector_ref = shift; | |||||||||||||
| 22 | 3 | ▶ | die _in_context 'Usage: FAIL_WITH $flag_opt, \%selector, @args' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | if ref $selector_ref ne 'HASH'; | ||||||||||||||
| 24 | } | ||||||||||||||
| 25 | die _in_context "Selector values must be sub refs" | ||||||||||||||
| 26 | 4 | ▶ | if grep {ref ne 'CODE'} values %{$selector_ref}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | # Search for handler sub; | ||||||||||||||
| 29 | 4 | my $handler; | |||||||||||||
| 30 | 4 | ▶ | if (defined $flag) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | ARG: | ||||||||||||||
| 32 | 3 | ▶ | while (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 7 | ▶ | last ARG if shift(@_) eq $flag; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | } | ||||||||||||||
| 35 | 3 | my $selector = shift @_; | |||||||||||||
| 36 | 3 | ▶ | if (ref $selector eq 'CODE') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 1 | $handler = $selector; | |||||||||||||
| 38 | 1 | @_ = (); | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | else { | ||||||||||||||
| 41 | 2 | @_ = $selector; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | SELECTION: | ||||||||||||||
| 46 | 4 | for my $selection (reverse @_) { | |||||||||||||
| 47 | 4 | ▶ | if (exists $selector_ref->{$selection}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | 2 | $handler = $selector_ref->{$selection}; | |||||||||||||
| 49 | 2 | last SELECTION; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | elsif ($flag) { | ||||||||||||||
| 52 | 1 | die _in_context "Invalid option: $flag => $selection"; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | # (Re)set handler... | ||||||||||||||
| 57 | 3 | ▶ | if ($handler) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 3 | my $caller_loc = join '|', (CORE::caller 1)[0,1]; | |||||||||||||
| 59 | 3 | ▶ | if (exists $handler_for{$caller_loc}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 2 | warn _in_context "FAIL handler for package ", scalar CORE::caller, " redefined"; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | 3 | $handler_for{$caller_loc} = $handler; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | }; | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 20 | ▶ | sub _FAIL (;&) { | ||||||||||||
|
Subroutine
_FAIL: called CC=6 SCAR=18.3
| |||||||||||||||
| 67 | # Generate args... | ||||||||||||||
| 68 | 20 | my $arg_generator_ref = shift; | |||||||||||||
| 69 | 20 | my @args; | |||||||||||||
| 70 | 20 | ▶ | if ($arg_generator_ref) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | package DB; | ||||||||||||||
| 72 | 5 | ()=CORE::caller(1); | |||||||||||||
| 73 | 5 | @args = $arg_generator_ref->(@DB::args); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | # Handle user-defined failure semantics... | ||||||||||||||
| 77 | 20 | my $caller_loc = join '|', (CORE::caller 1)[0,1]; | |||||||||||||
| 78 | 20 | ▶ | if (exists $handler_for{$caller_loc} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | # Fake out caller() and Carp... | ||||||||||||||
| 80 | 10 | local $Contextual::Return::uplevel = 1; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 10 | return $handler_for{$caller_loc}->(@args); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 10 | ▶ | my $exception = @args == 1 ? $args[0] | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 86 | : @args > 0 ? join(q{}, @args) | ||||||||||||||
| 87 | : "Call to " . (CORE::caller 1)[3] . "() failed" | ||||||||||||||
| 88 | ; | ||||||||||||||
| 89 | |||||||||||||||
| 90 | # Join message with croak() semantics, if string... | ||||||||||||||
| 91 | 10 | ▶ | if (!ref $exception) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 10 | $exception .= _in_context @_; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # # Check for immediate failure... | ||||||||||||||
| 96 | # use Want qw( want ); | ||||||||||||||
| 97 | # return 0 if want 'BOOL'; | ||||||||||||||
| 98 | # die $exception if !want 'SCALAR'; | ||||||||||||||
| 99 | |||||||||||||||
| 100 | # Return a delayed failure object... | ||||||||||||||
| 101 | return | ||||||||||||||
| 102 | 2 | ▶ | BOOL { 0 } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 103 | 8 | ▶ | DEFAULT { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=10.1
| |||||||||||||||
| 104 | 8 | ▶ | if (ref $exception) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | my $message = "$exception"; | |||||||||||||
| 106 | 0 | $message =~ s/$/\n/; | |||||||||||||
| 107 | 0 | die _in_context $message, "Attempted to use failure value"; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | else { | ||||||||||||||
| 110 | 8 | die _in_context $exception, "Attempted to use failure value"; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | 1 | ▶ | METHOD { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 114 | 1 | ▶ | error => sub { _in_context $exception } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 115 | 1 | } | |||||||||||||
| 116 | 10 | } | |||||||||||||
| 117 | |||||||||||||||
| 118 | 31 | 1; | |||||||||||||
| 119 | |||||||||||||||
| 120 | __END__ | ||||||||||||||