[Dancer-users] What about substitutions in config files?

Oleg A. Mamontov oleg at mamontov.net
Thu Sep 1 17:16:58 CEST 2011


Hello!

There is one useful feature in Catalyst::Plugin::ConfigLoader - substitutions before parsing config:

http://search.cpan.org/~bricas/Catalyst-Plugin-ConfigLoader-0.30/lib/Catalyst/Plugin/ConfigLoader.pm#config_substitutions(_$value_)

Sometimes it's comfortably to use paths relative to 'appdir' or 'public'.
Cwd (current working directory) depends on your setup (mod_per,plack,etc.)
In my case (plackup under supervise) cwd is under service (daemontools specific) directory.

I have adjusted Dancer::Config slightly:

--- Dancer/Config.pm.orig	2011-09-01 18:50:30.000000000 +0400
+++ Dancer/Config.pm	2011-09-01 19:00:54.000000000 +0400
@@ -200,7 +200,14 @@
 
     my $config;
 
-    eval { $config = YAML::LoadFile($file) };
+    my $yaml = do {
+        open my $fh, $file or
+            confess "Unable to load the configuration file: $file: $!";
+        local $/ = <$fh>;
+    };
+    $yaml =~ s/__setting\((appdir|confdir|public|views)\)__/setting($1)/ge;
+
+    eval { $config = YAML::Load($yaml) };
     if (my $err = $@ || (!$config)) {
         confess "Unable to parse the configuration file: $file: $@";
     }


This little patch allowed me to use relative paths in config.yml without worry
about my top-level setup:

engines:
    xslate:
        cache_dir: '__setting(appdir)__/var/xslate'
        path: [ '/','__setting(views)__' ]

photos:
    basedir: '__setting(appdir)__/photos'



What do you think about inclusion this functionality in Dancer core?


-- 
Cheers,
Oleg A. Mamontov

mailto:  oleg at mamontov.net

jabber:  lonerr at charla.mamontov.net
icq uin: 79-521-617
cell:    +7-903-798-1352



More information about the Dancer-users mailing list