On Sun, Oct 10, 2010 at 10:00:17PM +0200, "Poul H. Sørensen" wrote:
> Hi
>
> I'm trying to make a perl-module, where (some of) the attributes can be set
> by merging several configuration files, but
> I can't get it to work as expected/required:
>
> When using my module, I want it to be simple to create well-behaved
> applications (command line arguments overrides environment
> etc etc).
>
> Q1: Foo->new_with_options( )
> how do I pass the configuration file as an argument?
I believe that passing configfile => 'foo.conf' to new_with_options
should do the right thing. If it doesn't, that should probably be fixed.
> Q2: $foo->configfile( ) # or ->set_configfile( )
> how do I parse a configfile later on after constuction?
MooseX::ConfigFromFile (which MooseX::SimpleConfig uses) specifies that
->get_config_from_file returns a hashref of the config, which it then
uses to populate attributes. You should be able to call this directly,
and do whatever you want with the config hashref.
> Q3: --configfile answer1.xml --configfile answer2.xml
> how do I load and merge multiple configuration files?
There's not really a good interface for this that I'm aware of (but I've
used Hash::Merge in the past to implement this behavior in other
contexts).
> Q4: --configuration-file or --fille-de-configuration [pun intended]
> how do I call the command-line-flag anything else than "--configfile"?
has '+configfile' => (init_arg => 'something_else') might work, although
I haven't tried it. If it does, MooseX::Aliases might be a better
solution, since it will allow 'configfile' to continue to work also
(for constructor arguments anyway, I'm not sure how that interacts with
MooseX::Getopt).
> Q5: BUILD
> how do I load some default configuration files, before all the others?
sub BUILD {
my $self = shift;
my $default_config = $self->get_config_from_file('default.conf');
for my $attr ($self->meta->get_all_attributes) {
next if $attr->has_value($self);
next unless exists $default_config->{$attr->init_arg};
$attr->set_value($self, $default_config->{$attr->init_arg});
}
}
This is untested, but you should be able to get the idea.
> OK - I guess that not everything may be possible using (the current)
> MooseX::SimpleConfig and MooseX::Getopt,
> but I really hope that I missed out on something...
>
> Otherwise I may have a go at it myself, but I'm rather new to Moose, and
> wonder if a fair solutions is to
> implement an attribute 'configuration-file' isa ArrayRef with a trigger sub
> (still using MooseX::Getopt)?
> If so, how do I get new_with_options() to handle 'configuration_file' => [
> qw( answer.xml ) ] (it doesn't
> seem to invoke the trigger method (or setting the value) if their is a
> command-line option --configuratio_file).
It is true that the current implementations for things like this aren't
really ideal - they're all fairly old, and ConfigFromFile/SimpleConfig
are also unmaintained. A better overall implementation might be nice,
but you should talk about it either here or on IRC (#moose on
irc.perl.org) to make sure your ideas for how it should look are
reasonable(:
-doy