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)
26 sub _generate_stash_closure {
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} ||=
44 _generate_stash_closure;
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>. If the stash does not yet exist, we initialize
87 Expects: An object that does C<env> and arguments
89 Exportable subroutine.
91 Given an object with a method C<env> get or set stash values, either
92 as a method or via hashref modification. This stash is automatically
93 reset for each request (it is not persistent or shared across connected
94 clients. Stash key / value are stored in memory.
97 use Catalyst::Middleware::Stash 'stash';
101 my $req = Plack::Request->new($env);
102 my $stashed = $req->stash->{in_the_stash}; # Assume the stash was previously populated.
104 return [200, ['Content-Type' => 'text/plain'],
105 ["I found $stashed in the stash!"]];
108 If the stash does not yet exist, we initialize one and return that.
112 This class defines the following methods.
116 Used by plack to call the middleware