| 1 | package Call::Context; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =encoding utf-8 | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | Call::Context - Sanity-check calling context | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | use Call::Context; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | sub gives_a_list { | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | #Will die() if the context is not list. | ||||||||||||||||||||
| 16 | Call::Context::must_be_list(); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | return (1, 2, 3); | ||||||||||||||||||||
| 19 | } | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | gives_a_list(); #die()s: incorrect context (void) | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | my $v = gives_a_list(); #die()s: incorrect context (scalar) | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | my @list = gives_a_list(); #lives | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 DISCUSSION | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | If your function only expects to return a list, then a call in some other | ||||||||||||||||||||
| 30 | context is, by definition, an error. The problem is that, depending on how | ||||||||||||||||||||
| 31 | the function is written, it may actually do something expected in testing, but | ||||||||||||||||||||
| 32 | then in production act differently. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 FUNCTIONS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head2 must_be_list() | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | C<die()>s if the calling function is itself called outside list context. | ||||||||||||||||||||
| 39 | (See the SYNOPSIS for examples.) | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 EXCEPTIONS | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | This module throws instances of C<Call::Context::X>. C<Call::Context::X> is | ||||||||||||||||||||
| 44 | overloaded to stringify; however, to keep memory usage low, C<overload> is not | ||||||||||||||||||||
| 45 | loaded until instantiation. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 REPOSITORY | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | https://github.com/FGasper/p5-Call-Context | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 2 | our $VERSION = 0.02; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 2 | my $_OVERLOADED_X; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub must_be_list { | ||||||||||||||||||||
| 61 | 3 | ▶ | return if (caller 1)[5]; #wantarray | ||||||||||||||||||
|
Subroutine
must_be_list: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2 | ▶ | $_OVERLOADED_X ||= eval q{ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Condition66%2/3
$_OVERLOADED_X ||= eval "\n package Call::Context::X;\n use overload ( q<\"\"> => \\&_spew );\n 1;\n "
MC/DC0%0/2
$_OVERLOADED_X ||= eval "\n package Call::Context::X;\n use overload ( q<\"\"> => \\&_spew );\n 1;\n "
$_OVERLOADED_X
eval "\n package Call::Context::X;\n use overload ( q<\"\"> => \\&_spew );\n 1;\n "
Truth table66%2/3
A ||= B
A= $_OVERLOADED_XB= eval "\n package Call::Context::X;\n use overload ( q<\"\"> => \\&_spew );\n 1;\n "
| |||||||||||||||||||||
| 64 | package Call::Context::X; | ||||||||||||||||||||
| 65 | use overload ( q<""> => \\&_spew ); | ||||||||||||||||||||
| 66 | 1; | ||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | die Call::Context::X->_new(); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | package Call::Context::X; | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | #Not to be instantiated except from Call::Context! | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub _new { | ||||||||||||||||||||
| 79 | 2 | ▶ | my ($class) = @_; | ||||||||||||||||||
|
Subroutine
_new: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 2 | my ($sub, $ctx) = (caller 2)[3, 5]; | |||||||||||||||||||
| 82 | 2 | my (undef, $cfilename, $cline, $csub) = caller 3; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2 | ▶ | $ctx = defined($ctx) ? 'scalar' : 'void'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | return bless \"$sub called in non-list ($ctx) context from $csub (line $cline of $cfilename)", $class; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 4 | ▶ | sub _spew { ${ $_[0] } } | ||||||||||||||||||
|
Subroutine
_spew: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | 1; | |||||||||||||||||||