[Catalyst] C::M::{CDBI,DBIC} vs direct Loader use?

Jules Bean jules at jellybean.co.uk
Thu Nov 17 08:54:15 CET 2005


Brandon Black wrote:
> 
> The method of subclassing Catalyst::View::TT and pre-processing the
> stash variables in a custom process() method before calling
> NEXT->process() looked promising, but the issue there is that the
> stash can be many arbitrary levels of nested objects, arrays, hashes,
> etc, and I'd have to find a way to traverse them all in search of
> DateTime objects, which would be both ugly and expensive.
> 

This is an interesting discussion.

FWIW, this is how hard it is to iterate through your whole hash 
including array and hashrefs:

sub filter {
   my $value = shift;
   my $sub = shift;

   my $filtered = $sub->($value);
   return $filtered if ($filtered);

   if (ref $value eq 'HASH') {
     for my $k (keys %$value) {
       $value->{$k} = filter($value->{$k});
     }
   } elsif (ref $value eq 'ARRAY') {
     for my $i (0..$#$value) {
       $value->[$i] = filter($value->[$i]);
     }
   }
   return $value;
}

Use it like this:

$c->template->{stash} = filter($c->template->{stash},
sub { if (ref $_[0] eq 'DateTime') { return ... } else { return undef} }
);

Well it's untested, but that's the idea. Not too hard.

I think this may be a useful general notion. Might be worth 
Catalyst::Plugin::FilterStash.

J



More information about the Catalyst mailing list