4 package Catalyst::Middleware::Stash;
6 use base 'Plack::Middleware';
10 our @EXPORT_OK = qw(stash get_stash);
12 sub PSGI_KEY { 'Catalyst.Stash.v1' };
16 return $env->{&PSGI_KEY} ||
21 my ($host, @args) = @_;
22 return get_stash($host->env)
27 my $stash = shift || +{};
30 my $new_stash = @_ > 1 ? {@_} : $_[0];
31 croak('stash takes a hash or hashref')
32 unless ref $new_stash;
33 foreach my $key (keys %$new_stash) {
34 $stash->{$key} = $new_stash->{$key};
43 return $env->{&PSGI_KEY} ||=
48 my ($self, $env) = @_;
50 return $self->app->($env);
55 Catalyst::Middleware::Stash - The Catalyst stash - in middleware
59 We've moved the L<Catalyst> stash to middleware. Please don't use this
60 directly since it is likely to move off the Catalyst namespace into a stand
63 We store a coderef under the C<PSGI_KEY> which can be dereferenced with
64 key values or nothing to access the underly hashref.
68 This class defines the following subroutines.
72 Returns the hash key where we store the stash. You should not assume
73 the string value here will never change! Also, its better to use
74 L</get_stash> or L</stash>.
80 Exportable subroutine.
82 Get the stash out of the C<$env>.
86 Expects: An object that does C<env> and arguments
88 Exportable subroutine.
90 Given an object with a method C<env> get or set stash values, either
91 as a method or via hashref modification. This stash is automatically
92 reset for each request (it is not persistent or shared across connected
93 clients. Stash key / value are stored in memory.
96 use Catalyst::Middleware::Stash 'stash';
100 my $req = Plack::Request->new($env);
101 my $stashed = $req->stash->{in_the_stash}; # Assume the stash was previously populated.
103 return [200, ['Content-Type' => 'text/plain'],
104 ["I found $stashed in the stash!"]];
107 If the stash does not yet exist, we initialize one and return that.
111 This class defines the following methods.
115 Used by plack to call the middleware