Config::HAProxy - Parser for HAProxy configuration file


    use Config::HAProxy;
    $cfg = new Config::HAProxy($filename);

    $name = $cfg->filename;

    @frontends = $cfg->select(name => 'frontend');

    $itr = $cfg->iterator(inorder => 1);
    while (defined($node = $itr->next)) {
        # do something with $node



    $name = $self->backup_name;

    $node = $cfg->pop;
    $node = $cfg->tos;
    $node = $cfg->tree;


The Config::HAProxy class is a parser that converts the HAProxy configuration file to a parse tree and provides methods for various operations on this tree, such as: searching, modifying and saving it to a file.

An object of this class contains a parse tree representing the configuration read from the file (or created from scratch). Nodes in the tree can be of four distinct classes:


Represents an empty line.


Represents a comment line.


Represents a simple statement.


A container, representing a compound statement, i.e. a statement that contains multiple sub-statements. Compound statements are: global, defaults, frontend, and backend.

In addition to these four classes, a special class Root is provided, which represents the topmost node in the parse tree (i.e. the parent of other nodes).

A set of attributes is associated with each node. Among these, the orig attribute contains the original line from the configuration file that triggered creation of this node, and locus contains the location of this line (or lines, for sections) in the configuration file (as a Text::Locus) object.

These two attributes are meaningful for all nodes. For statement nodes (simple statements and sections) the kw attribute contains the statement keyword, and the argv attribute - its arguments. For example, the statement

    server localhost

is represented by a node of class Config::HAProxy::Node::Statement, with server as kw and list (localhost, as argv.

Additionally, section nodes provide methods for accessing their subtrees.

For a detailed description of the node class and its methods, please refer to Config::HAProxy::Node.


    $cfg = new Config::HAProxy($filename);

Creates and returns a new object for manipulating the HAProxy configuration. Optional $filename specifies the name of the file to read configuration from. It defaults to /etc/haproxy/haproxy.cfg.


    $s = $cfg->filename;

Returns the configuration file name given when creating the object.




Reads and parses the configuration file. Croaks if the file does not exist. Returns $cfg.




Clears the parse tree.



Appends the $node (the Config::HAProxy::Node object), to the end of the parse tree.


    $node = $cfg->pop;

Removes the tail node from the tree and returns it.



    $node = $cfg->tree;

Returns the top of the tree.


    $node = $cfg->tos;

Returns the last node in the tree.




Saves the parse tree in the configuration file.


    $cfg->write($file, %hash);

Writes configuration to the named file or file handle. First argument can be a file name, file handle or a string reference. If it is the only argument, the original indentation is preserved. Otherwise, if %hash controls the indentation of the output. It must contain at least the indent key, which specifies the amount of indentation per nesting level. If tabstop key is also present, its value must be a reference to the list of tabstop columns. For each statement with arguments, this array is consulted to determine the column number for each subsequent argument. Arguments are zero-indexed. Starting column where the argument should be placed is determined as $tabstop[$i], where $i is the argument index. Arguments with $i greater than or equal to @tabstop are appended to the resulting output, preserving their original offsets.

Normally, comments retain their original indentation. However, if the key reintent_comments is present, and its value is evaluated as true, then comments are reindented following the rules described above.


Config::HAProxy::Node, Config::HAProxy::Node::Section, Config::HAProxy::Node::Statement, Config::HAProxy::Iterator.


Sergey Poznyakoff, <>


Copyright (C) 2018 by Sergey Poznyakoff

This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library. If not, see <>.