line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
Cs 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
|
|
|
|
|
|
|
Cs 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. C is |
65
|
|
|
|
|
|
|
overloaded to stringify; however, to keep memory usage low, C 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
|
|
|
3
|
|
200922
|
use strict; |
|
3
|
|
|
|
|
26
|
|
|
3
|
|
|
|
|
89
|
|
75
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1009
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
our $VERSION = '0.03-TRIAL2'; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $_OVERLOADED_X; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub must_be_list { |
82
|
3
|
|
|
3
|
1
|
838
|
return _must_be_list(0); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub must_not_be_scalar { |
86
|
3
|
100
|
|
3
|
1
|
980
|
return if !defined( (caller 1)[5] ); |
87
|
2
|
|
|
|
|
8
|
return _must_be_list(1); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _must_be_list { |
91
|
5
|
100
|
|
5
|
|
36
|
return if (caller 2)[5]; #wantarray |
92
|
|
|
|
|
|
|
|
93
|
3
|
|
66
|
2
|
|
200
|
$_OVERLOADED_X ||= eval q{ |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
12
|
|
94
|
|
|
|
|
|
|
package Call::Context::X; |
95
|
|
|
|
|
|
|
use overload ( q<""> => \\&_spew ); |
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
|
99
|
3
|
|
|
|
|
12
|
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
|
|
|
3
|
|
8
|
my ($class, $accept_void_yn) = @_; |
110
|
|
|
|
|
|
|
|
111
|
3
|
|
|
|
|
21
|
my ($sub, $ctx) = (caller 3)[3, 5]; |
112
|
3
|
|
|
|
|
16
|
my (undef, $cfilename, $cline, $csub) = caller 4; |
113
|
|
|
|
|
|
|
|
114
|
3
|
100
|
|
|
|
12
|
if ($accept_void_yn) { |
115
|
1
|
|
|
|
|
13
|
return bless \"$sub called in scalar context from $csub (line $cline of $cfilename)", $class; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
2
|
100
|
|
|
|
7
|
$ctx = defined($ctx) ? 'scalar' : 'void'; |
119
|
|
|
|
|
|
|
|
120
|
2
|
|
|
|
|
16
|
return bless \"$sub called in non-list ($ctx) context from $csub (line $cline of $cfilename)", $class; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
7
|
|
|
7
|
|
945
|
sub _spew { ${ $_[0] } } |
|
7
|
|
|
|
|
141
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |