Commit | Line | Data |
98f0550c |
1 | NAME |
2 | MooseX::SimpleConfig - A Moose role for setting attributes from a simple |
3 | configfile |
4 | |
5 | SYNOPSIS |
6 | ## A YAML configfile named /etc/my_app.yaml: |
7 | foo: bar |
8 | baz: 123 |
9 | |
b80dd215 |
10 | ## In your class |
98f0550c |
11 | package My::App; |
12 | use Moose; |
b80dd215 |
13 | |
14 | with 'MooseX::SimpleConfig'; |
15 | |
16 | has 'foo' => (is => 'ro', isa => 'Str', required => 1); |
98f0550c |
17 | has 'baz' => (is => 'rw', isa => 'Int', required => 1); |
b80dd215 |
18 | |
19 | # ... rest of the class here |
20 | |
21 | ## in your script |
98f0550c |
22 | #!/usr/bin/perl |
b80dd215 |
23 | |
24 | use My::App; |
25 | |
26 | my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml'); |
98f0550c |
27 | # ... rest of the script here |
28 | |
29 | #################### |
30 | ###### combined with MooseX::Getopt: |
31 | |
b80dd215 |
32 | ## In your class |
98f0550c |
33 | package My::App; |
34 | use Moose; |
b80dd215 |
35 | |
36 | with 'MooseX::SimpleConfig'; |
98f0550c |
37 | with 'MooseX::Getopt'; |
b80dd215 |
38 | |
39 | has 'foo' => (is => 'ro', isa => 'Str', required => 1); |
98f0550c |
40 | has 'baz' => (is => 'rw', isa => 'Int', required => 1); |
b80dd215 |
41 | |
42 | # ... rest of the class here |
43 | |
44 | ## in your script |
98f0550c |
45 | #!/usr/bin/perl |
b80dd215 |
46 | |
47 | use My::App; |
48 | |
49 | my $app = My::App->new_with_options(); |
98f0550c |
50 | # ... rest of the script here |
51 | |
52 | ## on the command line |
53 | % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123 |
54 | |
55 | DESCRIPTION |
56 | This role loads simple configfiles to set object attributes. It is based |
57 | on the abstract role MooseX::ConfigFromFile, and uses Config::Any to |
58 | load your configfile. Config::Any will in turn support any of a variety |
59 | of different config formats, detected by the file extension. See |
60 | Config::Any for more details about supported formats. |
61 | |
62 | Like all MooseX::ConfigFromFile -derived configfile loaders, this module |
63 | is automatically supported by the MooseX::Getopt role as well, which |
64 | allows specifying "-configfile" on the commandline. |
65 | |
66 | ATTRIBUTES |
67 | configfile |
a96520dd |
68 | Provided by the base role MooseX::ConfigFromFile. You can provide a |
69 | default configfile pathname like so: |
70 | |
3690815f |
71 | has '+configfile' => ( default => '/etc/myapp.yaml' ); |
98f0550c |
72 | |
ebaa5f43 |
73 | You can pass an array of filenames if you want, but as usual the array |
74 | has to be wrapped in a sub ref. |
75 | |
3690815f |
76 | has '+configfile' => ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } ); |
ebaa5f43 |
77 | |
78 | Config files are trivially merged at the top level, with the right-hand |
79 | files taking precedence. |
80 | |
98f0550c |
81 | CLASS METHODS |
82 | new_with_config |
83 | Provided by the base role MooseX::ConfigFromFile. Acts just like regular |
84 | "new()", but also accepts an argument "configfile" to specify the |
85 | configfile from which to load other attributes. Explicit arguments to |
86 | "new_with_config" will override anything loaded from the configfile. |
87 | |
88 | get_config_from_file |
89 | Called internally by either "new_with_config" or MooseX::Getopt's |
90 | "new_with_options". Invokes Config::Any to parse "configfile". |
91 | |
92 | AUTHOR |
93 | Brandon L. Black, <blblack@gmail.com> |
94 | |
95 | LICENSE |
96 | This library is free software; you can redistribute it and/or modify it |
97 | under the same terms as Perl itself. |
98 | |