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);
44 my ($self, $env) = @_;
45 $self->_init_stash($env);
46 return $self->app->($env);
51 Catalyst::Middleware::Stash - The Catalyst stash - in middleware
55 We've moved the L<Catalyst> stash to middleware. Please don't use this
56 directly since it is likely to move off the Catalyst namespace into a stand
59 We store a coderef under the C<PSGI_KEY> which can be dereferenced with
60 key values or nothing to access the underly hashref.
64 This class defines the following subroutines.
68 Returns the hash key where we store the stash
72 Get the stash out of the C<$env>
76 Exportable subroutine.
78 Given an object with a method C<env> get or set stash values, either
79 as a method or via hashref modification. This stash is automatically
80 reset for each request (it is not persistent or shared across connected
81 clients. Stash key / value are stored in memory.
83 Catalyst::Middleware::Stash 'stash';
85 $c->stash->{foo} = $bar;
86 $c->stash( { moose => 'majestic', qux => 0 } );
87 $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref
89 =head2 generate_stash_closure
91 Creates the closure which is stored in the L<PSGI> environment.
95 This class defines the following methods.
99 Used by plack to call the middleware