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 Have you ever written this?
12
13 my ($result, @result);
14
15 # run a sub in the correct context
16 if(!defined wantarray){
17 some::code();
18 }
19 elsif(wantarray){
20 @result = some::code();
21 }
22 else {
23 $result = some::code();
24 }
25
26 # do something after some::code
27 $_ += 42 for (@result, $result);
28
29 # finally return the correct value
30 if(!defined wantarray){
31 return;
32 }
33 elsif(wantarray){
34 return @result;
35 }
36 else {
37 return $result;
38 }
39
40 Now you can just write this instead:
41
42 use Context::Preserve;
43
44 return preserve_context { some::code() }
45 after => sub { $_ += 42 for @_ };
46
48 Sometimes you need to call a function, get the results, act on the
49 results, then return the result of the function. This is painful
50 because of contexts; the original function can behave different if it's
51 called in void, scalar, or list context. You can ignore the various
52 cases and just pick one, but that's fragile. To do things right, you
53 need to see which case you're being called in, and then call the
54 function in that context. This results in 3 code paths, which is a
55 pain to type in (and maintain).
56
57 This module automates the process. You provide a coderef that is the
58 "original function", and another coderef to run after the original
59 runs. You can modify the return value (aliased to @_) here, and do
60 whatever else you need to do. "wantarray" is correct inside both
61 coderefs; in "after", though, the return value is ignored and the value
62 "wantarray" returns is related to the context that the original
63 function was called in.
64
66 "preserve_context"
67
69 preserve_context { original } [after|replace] => sub { after }
70 Invokes "original" in the same context as "preserve_context" was called
71 in, save the results, runs "after" in the same context, then returns
72 the result of "original" (or "after" if "replace" is used).
73
74 If the second argument is "after", then you can modify @_ to affect the
75 return value. "after"'s return value is ignored.
76
77 If the second argument is "replace", then modifying @_ doesn't do
78 anything. The return value of "after" is returned from
79 "preserve_context" instead.
80
81 Run "preserve_context" like this:
82
83 sub whatever {
84 ...
85 return preserve_context { orginal_function() }
86 after => sub { modify @_ };
87 }
88
89 or
90
91 sub whatever {
92 ...
93 return preserve_context { orginal_function() }
94 replace => sub { return @new_return };
95 }
96
97 Note that there's no comma between the first block and the "after =>"
98 part. This is how perl parses functions with the "(&@)" prototype.
99 The alternative is to say:
100
101 preserve_context(sub { original }, after => sub { after });
102
103 You can pick the one you like, but I think the first version is much
104 prettier.
105
107 Jonathan Rockway "<jrockway@cpan.org>"
108
109 Copyright (c) 2008 Infinity Interactive. You may redistribute this
110 module under the same terms as Perl itself.
111
112
113
114perl v5.12.0 2008-01-15 Context::Preserve(3)