1Context::Preserve(3) User Contributed Perl Documentation Context::Preserve(3)
2
3
4
6 Context::Preserve - Run code after a subroutine call, preserving the
7 context the subroutine would have seen if it were the last statement in
8 the caller
9
11 version 0.03
12
14 Have you ever written this?
15
16 my ($result, @result);
17
18 # run a sub in the correct context
19 if(!defined wantarray){
20 some::code();
21 }
22 elsif(wantarray){
23 @result = some::code();
24 }
25 else {
26 $result = some::code();
27 }
28
29 # do something after some::code
30 $_ += 42 for (@result, $result);
31
32 # finally return the correct value
33 if(!defined wantarray){
34 return;
35 }
36 elsif(wantarray){
37 return @result;
38 }
39 else {
40 return $result;
41 }
42
43 Now you can just write this instead:
44
45 use Context::Preserve;
46
47 return preserve_context { some::code() }
48 after => sub { $_ += 42 for @_ };
49
51 Sometimes you need to call a function, get the results, act on the
52 results, then return the result of the function. This is painful
53 because of contexts; the original function can behave different if it's
54 called in void, scalar, or list context. You can ignore the various
55 cases and just pick one, but that's fragile. To do things right, you
56 need to see which case you're being called in, and then call the
57 function in that context. This results in 3 code paths, which is a
58 pain to type in (and maintain).
59
60 This module automates the process. You provide a coderef that is the
61 "original function", and another coderef to run after the original
62 runs. You can modify the return value (aliased to @_) here, and do
63 whatever else you need to do. "wantarray" is correct inside both
64 coderefs; in "after", though, the return value is ignored and the value
65 "wantarray" returns is related to the context that the original
66 function was called in.
67
69 "preserve_context"
70
72 preserve_context { original } [after|replace] => sub { after }
73 Invokes "original" in the same context as "preserve_context" was called
74 in, save the results, runs "after" in the same context, then returns
75 the result of "original" (or "after" if "replace" is used).
76
77 If the second argument is "after", then you can modify @_ to affect the
78 return value. "after"'s return value is ignored.
79
80 If the second argument is "replace", then modifying @_ doesn't do
81 anything. The return value of "after" is returned from
82 "preserve_context" instead.
83
84 Run "preserve_context" like this:
85
86 sub whatever {
87 ...
88 return preserve_context { orginal_function() }
89 after => sub { modify @_ };
90 }
91
92 or
93
94 sub whatever {
95 ...
96 return preserve_context { orginal_function() }
97 replace => sub { return @new_return };
98 }
99
100 Note that there's no comma between the first block and the "after =>"
101 part. This is how perl parses functions with the "(&@)" prototype.
102 The alternative is to say:
103
104 preserve_context(sub { original }, after => sub { after });
105
106 You can pick the one you like, but I think the first version is much
107 prettier.
108
110 Bugs may be submitted through the RT bug tracker
111 <https://rt.cpan.org/Public/Dist/Display.html?Name=Context-Preserve>
112 (or bug-Context-Preserve@rt.cpan.org <mailto:bug-Context-
113 Preserve@rt.cpan.org>).
114
115 I am also usually active on irc, as 'ether' at "irc.perl.org".
116
118 Jonathan Rockway <jrockway@cpan.org>
119
121 · Karen Etheridge <ether@cpan.org>
122
123 · Jonathan Rockway <jon@jrock.us>
124
126 This software is copyright (c) 2008 by Infinity Interactive.
127
128 This is free software; you can redistribute it and/or modify it under
129 the same terms as the Perl 5 programming language system itself.
130
131
132
133perl v5.30.0 2019-07-26 Context::Preserve(3)