/usr/share/perl5/MouseX/ConfigFromFile.pm is in libmousex-configfromfile-perl 0.05-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | package MouseX::ConfigFromFile;
use 5.008_001;
use Mouse::Role;
use MouseX::Types::Path::Class;
our $VERSION = '0.05';
requires 'get_config_from_file';
has 'configfile' => (
is => 'ro',
isa => 'Path::Class::File',
coerce => 1,
predicate => 'has_configfile',
);
sub new_with_config {
my ($class, %params) = @_;
my $file = defined $params{configfile} ? $params{configfile} : do {
my $attr = $class->meta->get_attribute('configfile');
if ($attr->has_default) {
my $default = $attr->default;
ref($default) eq 'CODE' ? $default->($class) : $default;
}
elsif ($attr->has_builder) {
my $builder = $attr->builder;
$class->$builder();
}
else {
undef;
}
};
my %args = (
defined $file ? %{ $class->get_config_from_file($file) } : (),
%params,
);
return $class->new(%args);
}
no Mouse::Role; 1;
=head1 NAME
MouseX::ConfigFromFile - An abstract Mouse role for setting attributes from a configfile
=head1 SYNOPSIS
A real role based on this abstract role:
package MyApp::ConfigRole;
use Mouse::Role;
with 'MouseX::ConfigFromFile';
use MyApp::ConfigLoader;
sub get_config_from_file {
my ($class, $file) = @_;
my $config_hashref = MyApp::ConfigLoader->load($file);
return $config_hashref;
}
A class that uses it:
package MyApp;
use Mouse;
with 'MyApp::ConfigRole';
# optionally, default the configfile:
has '+configfile' => ( default => '/tmp/myapp.yml' );
A script that uses the class with a configfile:
my $app = MyApp->new_with_config(
configfile => '/etc/myapp.yml',
other_opt => 'foo',
);
=head1 DESCRIPTION
This is an abstract role which provides an alternate constructor for
creating objects using parameters passed in from a configuration file.
The actual implementation of reading the configuration file is left to
concrete subroles.
It declares an attribute C<configfile> and a class method
C<new_with_config>, and requires that concrete roles derived from it
implement the class method C<get_config_from_file>.
Attributes specified directly as arguments to C<new_with_config>
supercede those in the configfile.
=head1 METHODS
=head2 new_with_config(%params?)
This is an alternate constructor, which knows to look for the
C<configfile> option in its arguments and use that to set attributes.
It is much like L<MouseX::Getopts>' C<new_with_options>.
Example:
my $app = MyApp->new_with_config( configfile => '/etc/foo.yaml' );
Explicit arguments will override anything set by the configfile.
=head2 get_config_from_file($file)
This method is not implemented in this role, but it is required
of all subroles. Its two arguments are the class name and the configfile,
and it is expected to return a hashref of arguments to pass to C<new()>
which are sourced from the configfile.
Example:
sub get_config_from_file {
my ($class, $file) = @_;
my $config = {};
# ... load config from $file ...
return $config;
}
=head1 PROPERTIES
=head2 configfile
This is a L<Path::Class::File> object which can be coerced from a regular
path name string. This is the file your attributes are loaded from.
You can add a default configfile in the class using the role and it will
be honored at the appropriate time:
has '+configfile' => ( default => '/etc/myapp.yaml' );
=head1 AUTHOR
NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt>
=head1 THANKS TO
Brandon L. Black, L<MooseX::ConfigFromFile/AUTHOR>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<Mouse>, L<Mouse::Role>, L<MouseX::Types::Path::Class>, L<MooseX::ConfigFromFile>
=cut
|