The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Config::Role - Moose config attribute loaded from file in home dir

VERSION

version 0.0.1

SYNOPSIS

    package My::Class;
    use Moose;

    # Read configuration from ~/.my_class.ini, available in $self->config
    has 'config_filename' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
    sub _build_config_filename { '.my_class.ini' }
    with 'Config::Role';

    # Fetch a value from the configuration, allow constructor override
    has 'username' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
    sub _build_username { return (shift)->config->{'username'}; }

    sub make_request {
        my ($self) = @_;
        my $response = My::Class::Request->make(
            username => $self->username,
            ...
        );
        ...
    }

DESCRIPTION

Config::Role is a very basic role you can add to your Moose class that allows it to take configuration data from a file located in your home directory instead of always requiring parameters to be specified in the constructor.

The synopsis shows how you can read the value of username from the file .my_class.ini located in the home directory of the current user. The location of the file is determined by whatever File::HomeDir->my_data returns for your particular platform.

The config file is loaded by using Config::Any's load_files() method. It will load the files specified in the config_files attribute. By default this is an array reference that contains the filename from the config_file attribute. If you specify multiple files which both contain the same configuration key, the value is loaded from the first file. That is, the most significant file should be first in the array.

The Config::Any->load_files() flag use_ext is set to a true value, so you can use any configuration file format supported by Config::Any by just specifying the common filename extension for the format.

ATTRIBUTES

config_file

The filename the configuration is read from. A Path::Class::File object. Allows coercion from Str.

config_files

The collection of filenames the configuration is read from. Array reference of Path::Class::File objects. Allows coercion from an array reference of strings.

config

A hash reference that holds the compiled configuration read from the specified files.

METHODS

config_filename

Required method on the composing class. Should return a string with the name of the configuration file name.

SEMANTIC VERSIONING

This module uses semantic versioning concepts from http://semver.org/.

SEE ALSO

SUPPORT

Perldoc

You can find documentation for this module with the perldoc command.

  perldoc Config::Role

Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.

Bugs / Feature Requests

Please report any bugs or feature requests by email to bug-config-role at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Role. You will be automatically notified of any progress on the request by the system.

Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)

http://github.com/robinsmidsrod/Config-Role

  git clone git://github.com/robinsmidsrod/Config-Role.git

AUTHOR

Robin Smidsrød <robin@smidsrod.no>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Robin Smidsrød.

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