4 package Catalyst::Middleware::Stash;
6 use base 'Plack::Middleware';
8 use Scalar::Util 'blessed';
11 our @EXPORT_OK = qw(stash get_stash);
13 sub PSGI_KEY { 'Catalyst.Stash.v1' };
15 sub get_stash { return shift->{PSGI_KEY} }
17 sub generate_stash_closure {
18 my $stash = shift || +{};
21 my $new_stash = @_ > 1 ? {@_} : $_[0];
22 croak('stash takes a hash or hashref')
23 unless ref $new_stash;
24 foreach my $key ( keys %$new_stash ) {
25 $stash->{$key} = $new_stash->{$key};
33 my ($self, $env) = @_;
34 return $env->{PSGI_KEY} ||=
35 generate_stash_closure;
39 my ($host, @args) = @_;
40 return get_stash($host->env)->(@args) if
45 my ($self, $env) = @_;
46 $self->_init_stash($env);
47 return $self->app->($env);
52 Catalyst::Middleware::Stash - The Catalyst stash - in middleware
56 We've moved the L<Catalyst> stash to middleware. Please don't use this
57 directly since it is likely to move off the Catalyst namespace into a stand
60 We store a coderef under the C<PSGI_KEY> which can be dereferenced with
61 key values or nothing to access the underly hashref.
65 This class defines the following subroutines.
69 Returns the hash key where we store the stash
73 Get the stash out of the C<$env>
77 Exportable subroutine.
79 Given an object with a method C<env> get or set stash values, either
80 as a method or via hashref modification. This stash is automatically
81 reset for each request (it is not persistent or shared across connected
82 clients. Stash key / value are stored in memory.
84 Catalyst::Middleware::Stash 'stash';
86 $c->stash->{foo} = $bar;
87 $c->stash( { moose => 'majestic', qux => 0 } );
88 $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref
90 =head2 generate_stash_closure
92 Creates the closure which is stored in the L<PSGI> environment.
96 This class defines the following methods.
100 Used by plack to call the middleware