CGI-Application-Plugin-ConfigAuto

 view release on metacpan or  search on metacpan

lib/CGI/Application/Plugin/ConfigAuto.pm  view on Meta::CPAN


=head2 std_config()

L<CGI::Application::Standard::Config/std_config()> is implemented to comply with L<CGI::Application::Standard::Config>. It's
for developers. Users can ignore it. 

=cut

sub cfg {
    my $self = shift;

    if (!$self->{__CFG}) {
        require Config::Auto;

         unless ($self->{__CFG_FILES}) {
             my @all_cfg_files;
             for my $key (qw/cfg_file config_files/) {
                 my $cfg_file = $self->param($key);
                 if (defined $cfg_file) {
                     push @all_cfg_files, @$cfg_file  if (ref $cfg_file eq 'ARRAY');
                     push @all_cfg_files,  $cfg_file  if (ref \$cfg_file eq 'SCALAR');
                 }
             }
 
             # Non-standard call syntax for mix-in happiness.
             cfg_file($self,@all_cfg_files);
         }

        # Read in config files in the order the appear in this array.
        my %combined_cfg;
        for (my $i = 0; $i < scalar @{ $self->{__CFG_FILES} }; $i++) {
            my $file = $self->{__CFG_FILES}[$i];
            my %parms;
            if (ref $self->{__CFG_FILES}[$i+1] eq 'HASH') {
                %parms = %{ $self->{__CFG_FILES}[$i+1] };
                # skip trying to process the hashref as a file name
                $i++;
            }
            my $cfg = Config::Auto::parse($file, %parms);
            %combined_cfg = (%combined_cfg, %$cfg);
        }
        die "No configuration found. Check your config file(s) (check the syntax if this is a perl format)." 
            unless keys %combined_cfg;

        $self->{__CFG} = \%combined_cfg;
    }

    my $cfg = $self->{__CFG};
    my $field = shift;
    return $cfg->{$field} if $field;
    if (ref $cfg) {
        return wantarray ? %$cfg : $cfg;
    }
}

=head2 cfg_file()

 # Usual
 $self->cfg_file('my_config_file.pl');
    
 # Supply the first format, guess the second
 $self->cfg_file('my_config_file.pl',{ format => 'perl' } );

Supply an array of config files, and they will be processed in order.  If a
hash reference if found it, will be used to supply the format for the previous
file in the array.

=cut

sub cfg_file {
    my $self = shift;
    my @cfg_files = @_;
    unless (scalar @cfg_files) { croak "cfg_file: must have at least one config file." }
    $self->{__CFG_FILES} = \@cfg_files;
}


1;
__END__

=pod

=head1 FILE FORMAT HINTS

=head2  Perl

Here's a simple example of my favorite config file format: Perl.
Having the "shebang" line at the top helps C<Config::Auto> to identify
it as a Perl file. Also, be sure that your last statement returns a 
hash reference.

    #!/usr/bin/perl

    my %CFG = ();

    # directory path name
    $CFG{DIR} = '/home/mark/www';

    # website URL
    $CFG{URL} = 'http://mark.stosberg.com/';

    \%CFG;

=head1 SEE ALSO

L<CGI::Application|CGI::Application> 
L<CGI::Application::Plugin::ValidateRM|CGI::Application::Plugin::ValidateRM>
L<CGI::Application::Plugin::DBH|CGI::Application::Plugin::DBH>
L<CGI::Application::Standard::Config|CGI::Application::Standard::Config>.
perl(1)

=head1 AUTHOR

Mark Stosberg C<< mark@summersault.com >>

=head1 LICENSE

Copyright (C) 2004 - 2011 Mark Stosberg C<< mark@summersault.com >>

This library is free software. You can modify and or distribute it under the same terms as Perl itself.



( run in 0.701 second using v1.01-cache-2.11-cpan-39bf76dae61 )