I know this is old school for hardcore Perlheads, but I just learned how to use tie in perl.

While adding a generic config reader to Handel, I thought it would be nice to have the native config instance work just like using Apache::ModuleConfig does when using custom directives.

my $cfg = Apache::ModuleConfig->get($r);
my $setting = $cfg->{'Setting'};

The old config reader looks in two places for configuration. When running under mod_perl, it used $r→dir_config to get things set using PerlSetVar, otherwise it looks in the regular old %ENV.

With a little tie magic and an overridden FETCH method, I now have:

my $cfg = Handel::ConfigReader->new();
my $setting = $cfg->{'Setting'};

And since it’s a tied into Handel::ConfigReader, I can still use the previous method:

my $cfg = Handel::ConfigReader->new();
my $setting = $cfg->get('Setting', 'DefaultValue');

Not bad for a music guy.

See more posts about: perl | All Categories