[Catalyst-dev] PATCH for Catalyst::Plugin::ConfigLoader

Jason Kohles email at jasonkohles.com
Wed Aug 22 16:03:16 GMT 2007


On Aug 21, 2007, at 9:05 PM, Jonathan Rockway wrote:

> Sorry to hijack this thread... but it reminded me... something I've  
> been
> wanting for a while is:
>
>   $c->config->{substitutions} = {
>       foo => sub { bar($_[0]) . baz($_[1]) }
>       ...
>   }
>
> Then in the config file:
>
>    hello: __foo(Hello,World)__
>    ...
>
> Basically, let me specify my own macros in addition to (or overriding)
> __HOME__ and __path_to()__.  I'll write this, but if you want to do it
> instead, all the better :)
>
I actually did something similar to this a while back, I keep meaning  
to clean it up and document and tests, but you know how it goes,  
never enough TUITs  :)

I took a slightly different approach though, abstracting out the  
function that is applied to values, so you can subclass it...

sub finalize_config {
     my $c = shift;
     my $v = Data::Visitor::Callback->new(
         plain_value => sub {
             return unless defined $_;
             $c->config_substitutions( $_ );
         }
     );
     $v->visit( $c->config );
}

sub config_substitutions {
     my $c = shift;
     for ( @_ ) {
         s{__HOME__}{ $c->path_to( '' ) }e;
         s{__path_to\((.+)\)__}{ $c->path_to( split( '/', $1 ) ) }e;
     }
}


This way you can add a config_substitutions method to your app class  
for anything you want to add...

package MyApp;
sub config_substitutions {
	my $c = shift;
	$c->NEXT::config_substitutions( @_ );
	for ( @_ ) {
		s{__foo(.*?)__}{whatever};
	}
}


-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Catalyst-dev mailing list