MooX::ConfigFromFile::Role::SortedByFilename - allows filename based sort algorithm for multiple config files


  package MyApp::Cmd::TPau;

  use DBI;
  use Moo;
  use MooX::Cmd with_configfromfile => 1;
  with "MooX::ConfigFromFile::Role::SortedByFilename";
  with "MooX::ConfigFromFile::Role::HashMergeLoaded";

  # ensure hashes are merged by top-most scalar wins
  around _build_config_merge_behavior => sub { 'RIGHT_PRECEDENT' };

  has csv => (is => "ro", required => 1);

  sub execute
      my $self = shift;
      DBI->connect("DBI::csv:", undef, undef, $self->csv);

  $ cat etc/myapp.json
    "csv": {
      "f_ext": ".csv/r",
      "f_dir": "data"
      "csv_sep_char": ";",
      "csv_class": "Text::CSV_XS"
  $cat etc/myapp-tpau.json
    "csv": {
      "f_dir": "data/tpau"


This is an additional role for MooX::ConfigFromFile to allow merging loaded configurations in a more predictable order: filename > extension > path. (Read: When the filename is identical, the extensions are compared, when they are identical, the locations are compared).

While filename and file extension are compared on character basis, the sort order of the file locations (path) is based on precedence in MooX::File::ConfigDir or File::ConfigDir, respectively. This order is defined internally in @File::ConfigDir::extensible_bases.



This role modifies the builder for sorted_loaded_config by sorting the loaded files from raw_loaded_config by filename, extension and location in the filesystem, respectively.

Let's assume the affected setup has a CLI interface named oper with sub commands like git provides them, too. And the company using the application does it well by defining staging environments like DEV (Development), TEST (Testing), INT (Integration) and PROD (Production). For the example, the sub commands shall be deploy and report.

This will give you possible config_prefix_maps of

  # main command
  ['oper', 'dev']
  ['oper', 'test']
  ['oper', 'int']
  ['oper', 'prod']
  # deploy sub-command
  ['oper', 'deploy']
  ['oper', 'deploy', 'dev']
  ['oper', 'deploy', 'test']
  ['oper', 'deploy', 'int']
  ['oper', 'deploy', 'prod']
  # report sub-command
  ['oper', 'report']
  ['oper', 'report', 'dev']
  ['oper', 'report', 'test']
  ['oper', 'report', 'int']
  ['oper', 'report', 'prod']

This will result in (let's further assume, developers prefer JSON, operators prefer YAML) following possible config filenames:

    # main command
    # deploy sub-command
    # report sub-command

For a particular invoking (oper report in int stage) following files exists:

    '/etc/oper.json',                 # global configuration by developers
    '/etc/oper.yaml',                 # global configuration by operating policy
    '/opt/ctrl/etc/oper.json',        # vendor configuration by developers
    '/opt/ctrl/etc/oper.yaml',        # vendor configuration by operating policy
    '/opt/ctrl/etc/oper-int.yaml',    # vendor configuration by stage operating policy
    '/opt/ctrl/etc/oper-report.yaml', # vendor configuration by report operating team
    '/home/usr4711/oper-report.yaml', # usr4711 individual configuration (e.g. for template adoption)

The default sort algorithm will deliver


This role will change the sort algorithm to deliver


Which will cause that all policy configuration will override the developer defaults and user configuration override policy settings.


Jens Rehsack, <rehsack at>



Copyright 2018 Jens Rehsack.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.