NAME
Text::Substitute - Substitute in text
SYNOPSIS
use Text::Substitute;
my $ctl = { vars => { fn => "Johan", ln => "Bach" } };
say subst( $ctl, "The famous %{fn} %{ln}." );
DESCRIPTION
Text::Substitute provides a single function, subst
, that takes a string and substitutes variables by target texts.
The subroutine takes two arguments: a reference to a control hash and the string.
The variables are marked in the string by enclosing them between %}
and }
. For example, the string "The famous %{fn} %{ln}."
contains two variables, fn
and ln
.
In its basic form, the %{var}
is replaced by the value of the key var
in the vars
element of the control hash. It is also possible to specify replacement values depending on whether var
has a value or not:
"This book has %{title|title %{title}}"
"This book has %{title|title %{title}|no title}"
Assuming variable title
has the value C"My Book">, in the first example the text "title My Book"
will be substituted. If title
does not have a value, the empty string is substituted.
In the second example, the string "no title"
will be substituted.
As can be seen, the replacement texts may contain substitutions as well. For convenience, you can use %{}
to refer to the value of the variable being examinated. The last example above can be written more shortly and elegantly as:
"This book has %{title|title %{}|no title}"
You can test for specific values:
"This takes %{days=1|%{} day|%{} days}"
Finally, the values as specified in the control hash may be scalar (in general: strings and numbers) or lists of scalars. If a value is a list of scalars, it is possible to select a value from the list by appending a period and a number to the key. Assume customer
has value [ "Jones", "Smith" ]
, then:
"%{customer.1} will be Smith"
"%{customer.2} will be Jones"
"%{customer} will be Jones Smith"
The control hash contains the values for the variables in "vars"
:
{ vars => { customer => [ "Jones", "Smith" ],
days => 2, ... },
}
When list values need to be concatenated, a separator may be specified:
{ vars => { customer => [ "Jones", "Smith" ],
days => 2, ... },
separator => ", ",
}
EXPORT
subst
AUTHOR
Johan Vromans, <JV at CPAN dot org>
BUGS
Please report any bugs or feature requests to bug-text-substitute at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Substitute. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Text::Substitute
You can also look for information at:
RT: CPAN's request tracker
Search CPAN
ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
Copyright 2018 Johan Vromans, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.