|
NAMEString::Interpolate::Named - Interpolated named arguments in stringSYNOPSISuse String::Interpolate::Named; my $ctl = { args => { fn => "Johan", ln => "Bach" } }; say interpolate( $ctl, "The famous %{fn} %{ln}." ); # If you like object orientation. my $int = String::Interpolate::Named->new( { args => { ... } } ); say $int->interpolate("The famous %{fn} %{ln}."); DESCRIPTIONString::Interpolate::Named provides a function to interpolate named arguments by target texts in a template string. The target texts are provided to the function via a hash, where the keys correspond to the named argument to be replaced, or a subroutine that performs the lookup.Named ArgumentsThe arguments to be replaced are marked in the template by enclosing them between "%{" and "}". For example, the string "The famous %{fn} %{ln}." contains two named arguments, "fn" and "ln".Note that the activator may be changed from "%" into something else, see below. Throughout this document we use the default value. Basic InterpolationWhen interpolated, the keys "fn" and "ln" are looked up in the hash, and the corresponding values are substituted. If no value was found for a named argument, nothing is substituted and the "%{...}" is removed.You can precede "%", "{", "}" (and "|", see below) with a backslash "\" to hide their special meanings. For example, "\}" will not be considered closing an argument but yield a plain "}" in the text. Conditional InterpolationIt is possible to select replacement values depending on whether the named argument has a value or not:"This book has %{title|title %{title}}" "This book has %{title|title %{title}|no title}" These are considered "%{if|then}" and "%{if|then|else}" cases. Assuming argument "title" has the value "My Book", in the first example the text "title My Book", the 'then' text, will be substituted, resulting in "This book has title My Title" If "title" does not have a value, the empty string is substituted. In the second example, the string "no title", the 'else' text, will be substituted. As can be seen, the replacement texts may contain interpolations as well. For convenience, you can use "%{}" to refer to the value of the named argument currently being examinated. The last example above can be written more shortly and elegantly as: "This book has %{title|title %{}|no title}" Testing ValuesInstead of testing for named variables to have a value, you can also test for specific values:"This takes %{days=1|%{} day|%{} days}" List ValuesThe replacement values 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 particular value from the list by appending an index (period and a number) to the named argument.Assume "customer" has value "[ "Jones", "Smith" ]", then: "%{customer.1} will be Smith" "%{customer.2} will be Jones" "%{customer} will be Jones Smith" When the value exceeds the number of elements in the list, an empty value is returned. When no element is selected the values are concatenated. The Control HashThe interpolation process requires two parameters: a hash with settings and values for the named arguments, and the string to be used as a template for interpolation. The hash will be further referred to as the control hash.The hash can have the following keys:
An example of a control hash: my %ctl = ( args => { customer => [ "Jones", "Smith" ], days => 2, title => "My Title", }, separator => ", ", ); Object Oriented APImy $ii = String::Interpolate::Named->new; $ii->ctl(\%ctl); $result = $ii->interpolate($template); For convenience, the control hash may be passed to the constructor: my $ii = String::Interpolate::Named->new(\%ctl); $result = $ii->interpolate($template); Functional APIString::Interpolate::Named privides a single function, "interpolate", which is exported by default.The subroutine takes two arguments: a reference to a control hash and the template string. $result = interpolate( \%ctl, $template ); METHODSnewConstructs a new String::Interpolate::Named object.my $ii = String::Interpolate::Named->new; or my $ii = String::Interpolate::Named->new(\%ctl); ctlAssociates a control has with an existing object.$ii->ctl(\%ctl); interpolateThis routine performs the actual interpolations. It can be used as a method:$ii->interpolate($template); and functional: interpolate( \%ctl, $template ); REQUIREMENTSMinimal Perl version 5.10.1.AUTHORJohan Vromans, "<JV at CPAN dot org>"SUPPORTDevelopment of this module takes place on GitHub: <https://github.com/sciurius/perl-String-Interpolate-Named>.You can find documentation for this module with the perldoc command. perldoc String::Interpolate::Named Please report any bugs or feature requests using the issue tracker on GitHub. ACKNOWLEDGEMENTSMany of the existing template / interpolate / substitute modules.COPYRIGHT & LICENSECopyright 2018,2019 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.
Visit the GSP FreeBSD Man Page Interface. |