Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

context::preserve(3pm) [debian man page]

Context::Preserve(3pm)					User Contributed Perl Documentation				    Context::Preserve(3pm)

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 SYNOPSIS
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 @_ }; 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). This module automates the process. You provide a coderef that is the "original function", 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. "wantarray" is correct inside both coderefs; in "after", though, the return value is ignored and the value "wantarray" returns is related to the context that the original function was called in. EXPORT
"preserve_context" FUNCTIONS
preserve_context { original } [after|replace] => sub { after } Invokes "original" in the same context as "preserve_context" was called in, save the results, runs "after" in the same context, then returns the result of "original" (or "after" if "replace" is used). If the second argument is "after", then you can modify @_ to affect the return value. "after"'s return value is ignored. If the second argument is "replace", then modifying @_ doesn't do anything. The return value of "after" is returned from "preserve_context" instead. Run "preserve_context" 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 "after =>" part. This is how perl parses functions with the "(&@)" 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. AUTHOR AND COPYRIGHT
Jonathan Rockway "<jrockway@cpan.org>" Copyright (c) 2008 Infinity Interactive. You may redistribute this module under the same terms as Perl itself. perl v5.10.0 2008-01-15 Context::Preserve(3pm)

Check Out this Related Man Page

HTML::Mason::Plugin(3pm)				User Contributed Perl Documentation				  HTML::Mason::Plugin(3pm)

NAME
HTML::Mason::Plugin - Plugin Base class for Mason SYNOPIS
package MasonX::Plugin::Timer; use base qw(HTML::Mason::Plugin); use Time::HiRes; sub start_component_hook { my ($self, $context) = @_; push @{$self->{ timers }}, Time::HiRes::time; } sub end_component_hook { my ($self, $context) = @_; my $elapsed = Time::HiRes::time - pop @{$self->{ timers }}; printf STDERR "Component '%s' took %.1f seconds ", $context->comp->title, $elapsed; } 1; DESCRIPTION
Use a Mason plugin to have actions occur at the beginning or end of requests or components. Plugins are activated by passing plugins in the interpreter or request object. Each plugin in the list can be specified as a class name (in which case the plugin object is created once for each request) or as an actual object of the plugin class. If your plugin can be configured, place the configuration in class variables - for example, $MasonX::Plugin::Timer::Units = 'seconds'; These can be set either from httpd.conf via PerlSetVar directives, or in perl directly from a handler.pl file. PLUGIN HOOKS
A plugin class defines one or more of the following hooks (methods): start_request_hook, end_request_hook, start_component_hook, and end_component_hook. Every hook receives two arguments: the plugin object itself, and a context object with various methods. start_request_hook "start_request_hook" is called before the Mason request begins execution. Its context has the following read-only methods: request # the current request ($m) args # arguments the request was called with When called in scalar context, args returns a list reference which may be modified to change or add to the arguments passed to the first component. When called in list context, args returns a list (which may be assigned to a hash). Note that subrequests (see HTML::Mason::Request will create a new plugin object and execute this code again; you can skip your code for subrequests by checking "is_subrequest" on request. e.g. sub start_request_hook { my ($self, $context) = @_; unless ($context->request->is_subrequest()) { # perform hook action } } Currently, this hook is called before any information about the requested component is available, so you cannot call methods like "base_comp()" or "request_args()" on the Request object. end_request_hook "end_request_hook" is called before the Mason request exits. Its context has the following read-only methods: request # the current request ($m) args # arguments the request was called with output # reference to the contents of the output buffer wantarray # value of wantarray the request was called with result # arrayref of value(s) that the request is about to return error # reference to error, if any, that the request is about to throw When called in scalar context, args returns a list reference; when called in list context, it returns a list (which may be assigned to a hash). result always contains an array ref; if wantarray is 0, the return value is the the first element of that array. The plugin may modify output to affect what the request outputs, and result and error to affect what the request returns. start_component_hook "start_component_hook" is called before a component begins executing. Its context has the following read-only methods: request # the current request ($m) comp # the component object args # arrayref of arguments the component was called with The plugin may NOT modify args currently. end_component_hook "end_component_hook()" is called after a component has completed. Its context has the following read-only methods: request # the current request ($m) comp # the component object args # arrayref of arguments the component was called with wantarray # value of wantarray the component was called with result # arrayref of value(s) that the component is about to return error # reference to error, if any, that the component is about to throw result always contains an array ref; if wantarray is 0, the return value is the first element of that array. The plugin may modify both result and error to affect how the request returns. It would be desirable for this hook to have access to the component's output as well as its return value, but this is currently impossible because output from multiple components combine into a single buffer. WARNINGS
Do not keep an unweakened reference to a request or component object in your plugin object, or you will create a nasty circular reference. AUTHORS
Doug Treder, Jonathan Swartz, Dave Rolsky SEE ALSO
HTML::Mason::Interp, HTML::Mason::Request perl v5.14.2 2012-02-04 HTML::Mason::Plugin(3pm)
Man Page