Catalyst::Controller::BindLex - Stash your lexical goodness.
package MyApp::Controller::Moose; use base qw/Catalyst::Controller::BindLex/; sub bar : Local { my ( $self, $c ) = @_; my $x : Stashed; my %y : Stashed; $x = 100; do_something( $c->stash->{x} ); # 100 $c->forward( "gorch" ); } sub gorch : Private { my ( $self, $c ) = @_; my $x : Stashed; do_something( $x ); # still 100 } sub counter : Local { my ( $self, $c ) = @_; my $count : Session; $c->res->body( "request number " . ++$count ); }
This plugin lets you put your lexicals on the stash and elsewhere very easily.
It uses some funky modules to get it's job done: PadWalker, Array::RefElem, Devel::Caller, Devel::LexAlias and attributes. In some people's opinion this hurts this plugin's reputation ;-).
If you use the same name for two variables with the same storage binding attribute they will be aliased to each other, so you can use this for reading as well as writing values across controller subs. This is almost like sharing your lexical scope.
The way attributes are handled this can't be a plugin - the MODIFY_SCALAR_ATTRIBUTES methods and friends need to be in the class where the lexical is attributed, and this is typically a controller.
You can add attributes to the configaration by mapping attributes to handlers.
Handlers are either strings of methods to be called on $c with no arguments, which are expected to return a hash reference (like stash, session, etc), or code references invoked with $c, a reference to the variable we're binding, and the name of the variable we're binding, also expected to return a hash reference.
$c
stash
session
Some default attributes are pre-configured:
Bind the variable to a key in stash, session or flash respetively.
flash
The latter two require the use of
Matt S. Trout
Yuval Kogman
PadWalker, Array::RefElem, Devel::Caller, Devel::LexAlias, Sub::Parameters
Copyright (c) 2005 the aforementioned authors. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Catalyst::Controller::BindLex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Controller::BindLex
CPAN shell
perl -MCPAN -e shell install Catalyst::Controller::BindLex
For more information on module installation, please visit the detailed CPAN module installation guide.