If this is the case, then the macro expander should be instructed not to assert that the macro names encountered are valid for the object -- since CAN might fail, even though the calls will be handled by AUTOLOAD. To do this, pass a true value for the third value to the constructor:

my $macro_expander = Text::Macros->new( "\Q[[", "\Q]]", 1 );

Macros can take arguments. Any strings which occur inside the macro text after the macro name will be passed as arguments to the macro method call. By default, the macro name and any arguments are all separated by newlines. You can override this behavior; see the documentation of parse_args, below.

The delimiters are regular expressions; this gives you the greatest power in determining how macros are to be detected in the text. But it means that if you simply want them to be considered literal strings, then you must quotemeta them.

Since the macro expander will be calling object methods, you have an option: do you want any encountered macro names to be required to be valid for the given object? Or do you have some kind of autoloading in effect, which will handle undefined methods?

If you have some kind of autoloading, pass a true value for the third argument to new(). If you want the expander to assert CAN for each method, pass false (the default).

The fourth argument, $parse_args_cr, is a reference to a sub which implements your macro argument parsing policy. See the section on parse_args, below.

The $data_object argument is not an object of the Macros package. Rather, this is the object upon which the macro will be called as a method.

expand_macros() returns the result of replacing all the macros it finds with their appropriate expansions. Note that recursion can occur; that is, if the expansion of a macro results in text which also contains a valid macro, that new macro will also be expanded. The text will be scanned for macros, and those macros will be expanded, until none are found.

This is used internally by expand_macros(), but you can call it directly if you wish.

Essentially all this does is this:

$macro_expander->call_macro( $data_object, $macro_name, @arguments );

results in the call:

$data_object->$macro_name( @arguments );

All the macros supported by the data object can be predefined, or you might have some kind of autoloading mechanism in place for it. If you have autoloading in effect, you should have passed a true value as the third argument to new(). If you pass false (the default), the call_macro() will check to see that the object CAN do the method; and if it can't an exception will be thrown.

Note: data objects' macro methods must return a string. They can take any number of arguments, which will all be strings.

expand_macros tries to call the sub which was passed by reference as the fourth argument to new(), if there was one. If no such coderef was given to the constructor, then expand_macros calls the parse_args method in the Text::Macros class, which implements the default behavior of splitting the arg text on newlines, triming off leading/trailing whitespace, and then dropping any list elements which are '' (empty strings).

To implement some behavior other than the default, you may derive a class from Text::Macros which overrides parse_args. The parse_args method takes the Text::Macros object reference as the first arg (as usual), and the macro text as the second argument. This is all the text between the delimiters, as it occurs in the template text. This method is responsible for extracting the macro name and the values of any arguments from the macro text. It is advisable that the parse_args routine strip any leading and trailing whitespace from the argument values. (It happens automatically for the macro name, though, so you needn't worry about that.)

Alternatively, you may pass a code reference as the fourth argument to new(). The arguments to and results from this sub are the same as for the parse_args method, as described above, even though it is not (necessarily) a method itself.

The precedence is this: if a sub was passed to new(), that is called; if not, the parse_args() of the derived class is called, if defined; if not, the parse_args() of the base class (Text::Macros) is called.