Run code after a subroutine call, preserving the context the subroutine would have seen if it were the last statement in the caller
Have you ever written this?
my ($result, @result); # run a sub in the correct context if(!defined wantarray){ some::code(); } elsif(wantarray){ @result = some::code(); } else { $result = some::code(); } # do something after some::code $_ += 42 for (@result, $result); # finally return the correct value if(!defined wantarray){ return; } elsif(wantarray){ return @result; } else { return $result; }
Now you can just write this instead:
use Context::Preserve;
return preserve_context { some::code() } after => sub { $_ += 42 for @_ };
Sometimes you need to call a function, get the results, act on the results, then return the result of the function. This is painful because of contexts; the original function can behave different if it's called in void, scalar, or list context. You can ignore the various cases and just pick one, but that's fragile. To do things right, you need to see which case you're being called in, and then call the function in that context. This results in 3 code paths, which is a pain to type in (and maintain).
This module automates the process. You provide a coderef that is the \*(L"original function\*(R", and another coderef to run after the original runs. You can modify the return value (aliased to @_) here, and do whatever else you need to do. \*(C`wantarray\*(C' is correct inside both coderefs; in \*(L"after\*(R", though, the return value is ignored and the value \*(C`wantarray\*(C' returns is related to the context that the original function was called in.
\*(C`preserve_context\*(C'
Invokes \*(C`original\*(C' in the same context as \*(C`preserve_context\*(C' was called in, save the results, runs \*(C`after\*(C' in the same context, then returns the result of \*(C`original\*(C' (or \*(C`after\*(C' if \*(C`replace\*(C' is used).
If the second argument is \*(C`after\*(C', then you can modify @_ to affect the return value. \*(C`after\*(C''s return value is ignored.
If the second argument is \*(C`replace\*(C', then modifying @_ doesn't do anything. The return value of \*(C`after\*(C' is returned from \*(C`preserve_context\*(C' instead.
Run \*(C`preserve_context\*(C' like this:
sub whatever { ... return preserve_context { orginal_function() } after => sub { modify @_ }; }
or
sub whatever { ... return preserve_context { orginal_function() } replace => sub { return @new_return }; }
Note that there's no comma between the first block and the \*(C`after =>\*(C' part. This is how perl parses functions with the \*(C`(&@)\*(C' prototype. The alternative is to say:
preserve_context(sub { original }, after => sub { after });
You can pick the one you like, but I think the first version is much prettier.
Jonathan Rockway \*(C`<[email protected]>\*(C'
Copyright (c) 2008 Infinity Interactive. You may redistribute this module under the same terms as Perl itself.