The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

OpenInteract2::Config -- Centralized configuration information

SYNOPSIS

 use OpenInteract2::Config;
 
 my $config = OpenInteract2::Config->new( 'perl',
                                         '/path/to/dbi-config.info' );
 $config->{DEBUG} = 1;
 
 my $dbh = DBI->connect( $config->{db_dsn},
                         $config->{db_username},
                         $config->{db_password}
                         { RaiseError => 1 } );
 
 if ( my $debug = $config->{DEBUG} ) {
     print $LOG "Trace level $debug: fetching user $user_id...";
     if ( my $user = $self->fetch( $user_id ) ) {
         print $LOG "successful fetching $user_id\n";
     }
     else {
         print $LOG "No such user with ID $user_id";
     }
 }

DESCRIPTION

Simple configuration interface, used for the OpenInteract server configuration. Subclasses to serialize a configuration only have to implement two methods.

Once the configuration is read in you can access it like a hash:

 my ( $dsn, $uid, $pass ) = ( $config->{db_dsn},
                              $config->{db_username},
                              $config->{db_password} );

Setting values is similarly done:

 my $font_face = $config->{font_face} = 'Arial, Helvetica';

METHODS

A description of each method follows:

new( $type, @params )

Factory method to create the config object -- we take $type and see what implementation class is registered to it. The @params are passed to the read_config() method of the implementation and we bless the returned hashref to the correct class.

Note: we should probably lower case all arguments passed in, but getting/setting parameters and values should only be done via the interface. So, in theory, we should not allow the user to set anything here.

Returns: Configuration object.

is_file_valid( $filename )

Normally used by subclasses to see if a file exists. If not a standard error is thrown.

Returns: throws exception if $filename does not exist.

read_file( $filename )

Reads in $filename and returns a reference to the resulting array. If the file cannot be opened an exception is thrown.

Returns: arrayref of file contents, or throws exception if the file cannot be read.

translate_dirs()

This is generally only used on the server configuration file. Translates all entries under the configuration key 'dir' to be fully-qualified paths. The entry 'dir.website' must be set because an entry may have the expandable '$WEBSITE' key. In addition, no matter what OS you're on the entries are always forward-slash-separated so we can split them apart and pass the resulting list to File::Spec#catdir and create an OS-specific path.

This also sets the 'dir._IS_TRANSLATED_' key to true so we don't run the translation multiple times.

Returns: nothing

SUBCLASSING

Different configuration readers can register themselves with this class:

 OpenInteract2::Config->register_factory_type( mytype => 'My::Impl::Class' );

The class is not included until actually requested. See Class::Factory for details.

Subclasses must implement the following methods:

read_config( $filename )

Abstract method for subclasses to override with their own means of reading in config information (from DBI, file, CGI, whatever).

Returns: hashref of data read in on success; undef on failure

save_config()

Abstract method for subclasses to override with their own means of writing config to disk/eleswhere.

Returns: true on success; undef on failure.

COPYRIGHT

Copyright (c) 2001-2004 Chris Winters. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHORS

Chris Winters <chris@cwinters.com>