blib/lib/Call/Context.pm

filter: Statementstmt 100% 27 / 27 Branchbran 100% 8 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 94.0% 47 / 50 CC 6 SCAR 7.3
CC
6
Cov
97%
_must_be_list
3.0
_new
3.0
must_not_be_scalar
2.0
CRAP 2.1
↑ Summary
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 #----------------------------------------------------------------------
28  
29 sub scalar_is_bad {
30  
31 #Will die() if the context is not list.
32 Call::Context::must_not_be_scalar();
33  
34 return (1, 2, 3);
35 }
36  
37 scalar_is_bad(); # lives
38  
39 my $v = scalar_is_bad(); # die()s: incorrect context (scalar)
40  
41 my @list = scalar_is_bad(); # lives
42  
43 =head1 DISCUSSION
44  
45 If your function only expects to return a list, then a call in some other
46 context is, by definition, an error. The problem is that, depending on how
47 the function is written, it may actually do something expected in testing, but
48 then in production act differently.
49  
50 =head1 FUNCTIONS
51  
52 =head2 must_be_list()
53  
54 C<die()>s if the calling function is itself called outside list context.
55 (See the SYNOPSIS for examples.)
56  
57 =head2 must_not_be_scalar()
58  
59 C<die()>s if the calling function is itself called in scalar context.
60 (See the SYNOPSIS for examples.)
61  
62 =head1 EXCEPTIONS
63  
64 This module throws instances of C<Call::Context::X>. C<Call::Context::X> is
65 overloaded to stringify; however, to keep memory usage low, C<overload> is not
66 loaded until instantiation.
67  
68 =head1 REPOSITORY
69  
70 https://github.com/FGasper/p5-Call-Context
71  
72 =cut
73  
74 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 3 our $VERSION = '0.03';
78  
79 3 my $_OVERLOADED_X;
80  
81 sub must_be_list {
82 3 ▶ return _must_be_list(0);
Subroutine must_be_list: called CC=1 SCAR=0.0
Pod documented
83 }
84  
85 sub must_not_be_scalar {
86 3 ▶ return if !defined( (caller 1)[5] );
Subroutine must_not_be_scalar: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined((caller 1)[5]) 1 2 3
87 2 return _must_be_list(1);
88 }
89  
90 sub _must_be_list {
91 5 ▶ return if (caller 2)[5]; #wantarray
Subroutine _must_be_list: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (caller 2)[5] 2 3 5
92  
93 3 ▶ $_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 "
l!l&&r!l&&!r
120
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 "
ABresult
1X1
011
000
94 package Call::Context::X;
95 use overload ( q<""> => \\&_spew );
96 1;
97 };
98  
99 3 die Call::Context::X->_new($_[0]);
100 }
101  
102 #----------------------------------------------------------------------
103  
104 package Call::Context::X;
105  
106 #Not to be instantiated except from Call::Context!
107  
108 sub _new {
109 3 ▶ my ($class, $accept_void_yn) = @_;
Subroutine _new: called CC=3 SCAR=11.0
110  
111 3 my ($sub, $ctx) = (caller 3)[3, 5];
112 3 my (undef, $cfilename, $cline, $csub) = caller 4;
113  
114 3 ▶ if ($accept_void_yn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accept_void_yn) 1 2 3
115 1 return bless \"$sub called in scalar context from $csub (line $cline of $cfilename)", $class;
116 }
117  
118 2 ▶ $ctx = defined($ctx) ? 'scalar' : 'void';
Branch: 1 branch
BranchTrueFalseTotal
defined $ctx ? : 1 1 2
119  
120 2 return bless \"$sub called in non-list ($ctx) context from $csub (line $cline of $cfilename)", $class;
121 }
122  
123 7 ▶sub _spew { ${ $_[0] } }
Subroutine _spew: called CC=1 SCAR=0.0
124  
125 3 1;
↑ Summary