package Catalyst::Plugin::Stash::Filter; use warnings; use strict; use NEXT; 1; package Catalyst::Engine; use warnings; use strict; use NEXT; =item $c->stash If the first argument is a hashref, returns a hashref containing only data keys that matches $_[0]->{FILTER}. Otherwise, returns a hashref containing all your data. $c->stash->{foo} ||= 'yada'; print $c->stash->{foo}; =cut sub stash { my $self = shift; # this makes the real stash() method magic... my $stash = $self->NEXT::stash(); # this filters out everything unwanted or unknow... if( UNIVERSAL::isa( $_[0], 'HASH') && exists $_[0]->{FILTER} ){ # $strip is just a boolean. my( $criteria, $strip ) = ( $_[0]->{FILTER}, $_[0]->{STRIP} ); ## err... "borrowed" from Andy Wardley's AppConfig::State module. ## Original comments follow. ## thanks, Andy. # extract relevant keys and slice out corresponding values my @keys = grep(/$criteria/, keys %{ $stash }); my @vals = @{ $stash }{ @keys }; my %set; # clean off the $criteria part if $strip is set @keys = map { s/$criteria//; $_ } @keys if $strip; # slice values into the target hash @set{ @keys } = @vals; $stash = \%set; } return $stash; } 1;