Help language development. Donate to The Perl Foundation

Config::BINDish zef:vrurg last updated on 2023-03-02

`Config::BINDish` - parse BIND9/named kind of config files


    my $cfg =;
    $ string => q:to/CFG/;
    server "s1" {
        name "my.server";
        paths {
            base "/opt/myapp";
            pool "files" static {
            pool "files" dynamic {
    say $ :server("s1") => :paths => :pool("images") => 'root' ); # ./pub/img

    $ file => $my-config-filename;


This module is very much experimental in the sense of the API methods it provides. The grammar is expected to be more stable, yet no warranties can be given at the moment.


In this documentation I'll be referring to the configuration format implemented by the module as *BINDish config* or simply *BINDish*.


BINDish configuration parser can be augmented with 3rd-party extensions. Every extension is implemented as a role which will be used to build the final grammar or actions classes (see [`Grammar`]( The classes are based upon [`Config::BINDish::Grammar`](BINDish/ and [`Config::BINDish::Actions`](BINDish/ respectively. Here is the steps `Config::BINDish` does to build them:

  * An empty class is created which will serve as the final version

  * Extension roles are punned and added as parents to the class

  * Then the base class is added as the last parent

The order in which the extensions are added is defined by the order and the way they're registered. The later added ones serve as the earlier parents meaning that Raku's method call dispatching will have their methods invoked first.

There are two and a half ways to extend the parser. First is to use `is BINDish-grammar` or `is BINDish-actions` trait:

    unit module Config::BINDish::Ext1;
    use Config::BINDish;
    role Grammar is BINDish-grammar {
        token value:sym<mine> {
    role Actions is BINDish-actions {
        method value:sym<mine>($/) {

In this case the role they're applied to will be auto-registered with `Config::BINDish`. When such extension is contained by a module then it would be added when the module is `use`d:

    use Config::BINDish::Ext1;
    use Config::BINDish::Ext2;

*Note* that considering the order of `use` statements, `Ext2` will be able to override methods of `Ext1`.

The specificifty of using the traits is that extensions declared this way will become application-wide available. So, even if the extension module is used by a module used by the main code, the extension will be available to any instance of `Config::BINDish`.

**Note:** We say that extensions registered with the traits are registered *statically*.

The other 1.5 ways of adding the extensions are to use `extend-grammar` and `extend-actions` constructor arguments or methods with the same names:

    my $cfg = :extend-grammar(ExtG1, ExtG2), :extend-actions(ExtA1, ExtA2);

In this case extension roles don't need the traits applied. This way we call *dynamic* registration.

The other specific of dynamic extensions is that they will go after the static ones. I.e. in the above examples `ExtG*` and `ExtA*` will be positioned before `Ext1` and `Ext2` in the MRO order, prioritizing the former over the latter ones.

Why is the above called *1.5 ways*? Because the constructor eventually uses the `extend-*` methods with corresponding `:extend-*` named attributes.

See also [`Config::BINDish::Grammar`](BINDish/ and [`Config::BINDish::Actions`](BINDish/


[`Config::BINDish::Grammar::Strictness`](BINDish/Grammar/ `$.strict`

The default grammar strictness mode. See [`Config::BINDish::Grammar::Strictness`](BINDish/Grammar/ documentation for details.

[`Pair:D`]( `@.blocks`, [`Pair:D`]( `@.options`

These two attributes contain user-defined (pre-declared) structure of the config file. More information about them can be found in [Config::BINDish::Grammar](BINDish/ documentation, in [Pre-declaration](BINDish/ section.

`$.grammar`, `$.actions`

The final grammar and actions class versions with all registered extensions applied. Both attributes are *lazy* and `clearable` in terms of [`AttrX::Mooish`]( It means that the following is possible:

    say $cfg.grammar.^name; # Config::BINDish::Grammar...
    $cfg.clear-grammar;     # Actually, extend-grammar already does this. This line is here to demo the concept only.
    say $cfg.grammar.^name; # Config::BINDish::Grammar+{MyApp::GrammarMod}...

[`IO::Path`]( `$.file`

If `read` method was called with `:file<...>` argument then this attribute will hold corresponding `IO::Path` object for the file name.

[`Bool`]( `$.flat = False`

If set to `True` then [`Config::BINDish::Actions`](BINDish/ will act in flattening mode.


The top node produced by the grammar actions. I.e. it is the result of `$<TOP>.ast` of the [`Match`]( object produced by grammar's `parse` method. For [`Config::BINDish::Actions`](BINDish/ it would be an instance of [`Config::BINDish::AST::TOP`](BINDish/AST/ But an extension can produce something to its taste which wouldn't be an AST whatsoever. The only requirement imposed on the object stored in the attribute is to provide `get` method.

This attribute `handles` method `get`.


This attribute stores the [`Match`]( object produced by grammar's `parse` method.


`extend-grammar( [email protected] )`, `extend-actions( [email protected] )`

Interface to dynamically register extensions. Take a list of roles and records them as extensions. Then it clears `$.grammar` or `$.actions` attributes, respectively. Both return `self` to allow method call chaining.

`build-grammar()`, `build-actions()`

Methods used by [`AttrX::Mooish`]( to lazily initialize `$.grammar` and `$.actions` attributes respectively.

`multi method read(Config::BINDish:U: |args)`

Instantiates `Config::BINDish` class and re-invokes `read` method on the instance with `args` capture.

`multi method read(IO:D(Str:D) :$file, |args)`, `multi method read(Str:D :$string, |args)`

Parses a configuration stored either in a `$string` or in a `$file` and returns the resulting [`Match`]( object. The capture `args` is passed over to the `parse` method of `$.grammar` alongside with `$.actions`, `$.strict`, `$.flat`, `%.blocks`, and `%.options` attributes.

The method returns what is returned by grammar's `parse` method. The same value is then stored in `$.match` attribute.

### `multi get(...)`

Method is handled by `$.top` attribute. See [`Config::BINDish::AST::Blockish`](BINDish/AST/ for detailed method description.


By default this module exports only `BINDish-grammar` and `BINDish-actions` traits. But if `use`d with either "op" or "ascii-op" positional arguments it will also export _request operator_ in either unicode or ASCII form:

    use Config::BINDish <op>;
    my $cfg =;
    say $cfg ∷ :top-block<name> ∷ "option";


    use Config::BINDish <ascii-op>;
    my $cfg =;
    say $cfg :: :top-block<name> :: "option";

*Note* that `::` (ASCII version) may conflict with Raku's name resolution. Though in my tests this never happened, I would still prefer the unicode version over the ASCII.

More information about the operator can be found in [`Config::BINDish::Ops`](BINDish/



[`Config::BINDish::Grammar`](BINDish/, [`Config::BINDish::AST`](BINDish/, [`Config::BINDish::Ops`](BINDish/, [`Config::BINDish::Expandable`](BINDish/, [`Config::BINDish::INET`](BINDish/


Vadim Belman <[email protected]>