1 package Context::Preserve;
7 our @EXPORT = qw(preserve_context);
11 sub preserve_context(&@) {
15 my $replace = $args{replace};
16 my $after = $args{after};
18 croak 'need an "after" or "replace" coderef'
19 unless $replace || $after;
21 if(!defined wantarray){
32 my @result = $orig->();
34 my @ignored = $after->(@result);
37 @result = $replace->(@result);
42 my $result = $orig->();
44 my $ignored = $after->($result);
47 $result = $replace->($result);
58 Context::Preserve - run code after a subroutine call, preserving the context the subroutine would have seen if it were the last statement in the caller
62 Have you ever written this?
64 my ($result, @result);
66 # run a sub in the correct context
67 if(!defined wantarray){
71 @result = some::code();
74 $result = some::code();
77 # do something after some::code
78 $_ += 42 for (@result, $result);
80 # finally return the correct value
81 if(!defined wantarray){
91 Now you can just write this instead:
93 use Context::Preserve;
95 return preserve_context { some::code() }
96 after => sub { $_ += 42 for @_ };
100 Sometimes you need to call a function, get the results, act on the
101 results, then return the result of the function. This is painful
102 because of contexts; the original function can behave different if
103 it's called in void, scalar, or list context. You can ignore the
104 various cases and just pick one, but that's fragile. To do things
105 right, you need to see which case you're being called in, and then
106 call the function in that context. This results in 3 code paths,
107 which is a pain to type in (and maintain).
109 This module automates the process. You provide a coderef that is the
110 "original function", and another coderef to run after the original
111 runs. You can modify the return value (aliased to @_) here, and do
112 whatever else you need to do. C<wantarray> is correct inside both
113 coderefs; in "after", though, the return value is ignored and the
114 value C<wantarray> returns is related to the context that the original
115 function was called in.
123 =head2 preserve_context { original } [after|replace] => sub { after }
125 Invokes C<original> in the same context as C<preserve_context> was
126 called in, save the results, runs C<after> in the same context, then
127 returns the result of C<original> (or C<after> if C<replace> is used).
129 If the second argument is C<after>, then you can modify C<@_> to
130 affect the return value. C<after>'s return value is ignored.
132 If the second argument is C<replace>, then modifying C<@_> doesn't do
133 anything. The return value of C<after> is returned from
134 C<preserve_context> instead.
136 Run C<preserve_context> like this:
140 return preserve_context { orginal_function() }
141 after => sub { modify @_ };
148 return preserve_context { orginal_function() }
149 replace => sub { return @new_return };
153 Note that there's no comma between the first block and the C<< after
154 => >> part. This is how perl parses functions with the C<(&@)>
155 prototype. The alternative is to say:
157 preserve_context(sub { original }, after => sub { after });
159 You can pick the one you like, but I think the first version is much
162 =head1 AUTHOR AND COPYRIGHT
164 Jonathan Rockway C<< <jrockway@cpan.org> >>
166 Copyright (c) 2008 Infinity Interactive. You may redistribute this
167 module under the same terms as Perl itself.