.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Context::Preserve 3pm" .TH Context::Preserve 3pm "2022-10-13" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" 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 .SH "VERSION" .IX Header "VERSION" version 0.03 .SH "SYNOPSIS" .IX Header "SYNOPSIS" Have you ever written this? .PP .Vb 1 \& 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; \& } .Ve .PP Now you can just write this instead: .PP .Vb 1 \& use Context::Preserve; \& \& return preserve_context { some::code() } \& after => sub { $_ += 42 for @_ }; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" 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). .PP 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 \f(CW@_\fR) here, and do whatever else you need to do. \f(CW\*(C`wantarray\*(C'\fR is correct inside both coderefs; in \*(L"after\*(R", though, the return value is ignored and the value \f(CW\*(C`wantarray\*(C'\fR returns is related to the context that the original function was called in. .SH "EXPORT" .IX Header "EXPORT" \&\f(CW\*(C`preserve_context\*(C'\fR .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "preserve_context { original } [after|replace] => sub { after }" .IX Subsection "preserve_context { original } [after|replace] => sub { after }" Invokes \f(CW\*(C`original\*(C'\fR in the same context as \f(CW\*(C`preserve_context\*(C'\fR was called in, save the results, runs \f(CW\*(C`after\*(C'\fR in the same context, then returns the result of \f(CW\*(C`original\*(C'\fR (or \f(CW\*(C`after\*(C'\fR if \f(CW\*(C`replace\*(C'\fR is used). .PP If the second argument is \f(CW\*(C`after\*(C'\fR, then you can modify \f(CW@_\fR to affect the return value. \f(CW\*(C`after\*(C'\fR's return value is ignored. .PP If the second argument is \f(CW\*(C`replace\*(C'\fR, then modifying \f(CW@_\fR doesn't do anything. The return value of \f(CW\*(C`after\*(C'\fR is returned from \&\f(CW\*(C`preserve_context\*(C'\fR instead. .PP Run \f(CW\*(C`preserve_context\*(C'\fR like this: .PP .Vb 5 \& sub whatever { \& ... \& return preserve_context { orginal_function() } \& after => sub { modify @_ }; \& } \& \& or \& \& sub whatever { \& ... \& return preserve_context { orginal_function() } \& replace => sub { return @new_return }; \& } .Ve .PP Note that there's no comma between the first block and the \f(CW\*(C`after =>\*(C'\fR part. This is how perl parses functions with the \f(CW\*(C`(&@)\*(C'\fR prototype. The alternative is to say: .PP .Vb 1 \& preserve_context(sub { original }, after => sub { after }); .Ve .PP You can pick the one you like, but I think the first version is much prettier. .SH "SUPPORT" .IX Header "SUPPORT" Bugs may be submitted through the \s-1RT\s0 bug tracker (or bug\-Context\-Preserve@rt.cpan.org ). .PP I am also usually active on irc, as 'ether' at \f(CW\*(C`irc.perl.org\*(C'\fR. .SH "AUTHOR" .IX Header "AUTHOR" Jonathan Rockway .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Karen Etheridge .IP "\(bu" 4 Jonathan Rockway .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2008 by Infinity Interactive. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.