Application-Config

 view release on metacpan or  search on metacpan

lib/Application/Config.pm  view on Meta::CPAN

package Application::Config;

use strict;
use warnings;

use Config::Tiny;
use File::HomeDir;

our $VERSION = '0.5';
our $CONFIG  = {};
our $FILE    = {};

sub import {
  my $class   = shift;
  my $cfgfile = shift;
  my $pkg     = shift;
  
  if (!$pkg) {
    ($pkg, undef, undef) = caller();
  }
  
  if (!$cfgfile) {
    my $cfgpkg = substr($pkg, rindex($pkg, ':') + 1);
    $cfgfile = lc($cfgpkg) . ".conf" ;
  }
  
  {
    no strict;

    *{$pkg."::config"} = sub {
      return $CONFIG{$pkg} if $CONFIG{$pkg};
      my @paths = (
        File::Spec->catfile( File::HomeDir->my_home, ".$cfgfile" ),
        ".$cfgfile",
        "./etc/$cfgfile",
        "/etc/$cfgfile"
      );
      foreach my $path (@paths) {
        if (-e $path) {
          $FILE{$pkg} = $path;
          return $CONFIG{$pkg} = Config::Tiny->read( $path );
        }
      }
      return {};
    };    

    *{$pkg.'::configfile'} = sub {
      return $FILE{$pkg};
    };
    
    *{$pkg."::pkgconfig"} = sub {
      my ($package, $filename, $line) = caller;
      return $pkg->config->{$package} || {};
    };
  }
  
}

=head1 NAME

  Application::Config - configuration for applications that makes less work
  
=head1 SYNOPSIS

  package Foo;
  
  use Application::Config;
  
  package Some::Other::Package;
  
  my $config = Foo->config;
  
=head1 DESCRIPTION

I find myself writing methods that fetch a config file for an application from
disk and return it all the time.  I come from the small-tools loosely joined school of thought,
which means I'm writing little applications all the time. This really sucks.  Application::Config solves that problem
for me.  It might solve it for you.  Who knows.



( run in 1.346 second using v1.01-cache-2.11-cpan-437f7b0c052 )