String...:Named(3pm)
π¦ NAME π¦
String::Interpolate::Named β ok bestie, it does interpolated named arguments in strings. That's the whole vibe. It's from the User Contributed Perl Documentation, section 3pm, and no cap it's lowkey iconic.
β¨ SYNOPSIS β¨
use 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}.");
π DESCRIPTION π
So basically String::Interpolate::Named gives you a function that interpolates named arguments by target texts inside a template string. You hand over those target texts as a hash β the keys line up with the named argument that's gonna get replaced β or you can pass a subroutine that does the lookup for you. Choose your fighter. π«Ά
π Named Arguments π
You mark the stuff that's gonna be replaced in the template by wrapping it between %{ and }. Like, the string "The famous %{fn} %{ln}." has two named arguments in it: fn and ln. Periodt.
Heads up though β the activator can be switched from % to something else, scroll down for that. But throughout this whole document we're rolling with the default value.
πΈ Basic Interpolation πΈ
When it interpolates, the keys fn and ln get looked up in the hash, and the matching values get substituted in. If there's no value found for a named argument, nothing gets substituted and the %{...} just gets yeeted out entirely.
Oh and you can put a backslash \ in front of %, {, } (and |, more on that below) to hide what they mean. Like, \} won't count as closing an argument, it'll just give you a plain } in the text. Slay.
π§ Conditional Interpolation π§
You can pick which replacement value shows up 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 the %{if|then} and %{if|then|else} cases, respectively.
Say the argument title has the value My Book. In the first example, the text title My Book β that's the 'then' text β gets substituted, giving you
"This book has title My Title"
If title does not have a value, the empty string gets substituted. And in the second example, the string no title β the 'else' text β gets substituted instead.
As you can see, the replacement texts can contain interpolations too. And for convenience you can use %{} to refer to the value of the named argument that's currently being examined. So that last example above can be written shorter and way more elegantly as:
"This book has %{title|title %{}|no title}"
π₯ Testing Values π₯
Instead of testing whether named variables have a value, you can also test for specific values:
"This takes %{days=1|%{} day|%{} days}"
π List Values π
The replacement values hash can hold scalars (generally: strings and numbers) or lists of scalars. If a value is a list of scalars, you can select one particular value from the list by tacking an index onto the named argument β that's a period and a number.
Say customer has the value [ "Jones", "Smith" ], then:
"%{customer} will be Jones Smith"
"%{customer.0} will be Jones Smith"
"%{customer.1} will be Jones"
"%{customer.2} will be Smith"
When the value goes past the number of elements in the list, you get an empty value back. Index zero, or no index at all, returns all the values concatenated. π
π Format modifiers π
A named variable can have format modifiers attached to it to perform formatting operations on the substituted value. Format modifiers start with a colon :.
Say argument title has the value My Book, then %{title:lc} will give you that title in lowercase, my book.
Here's the full menu of format modifiers:
:lcβ yields the substituted value in all lower case.
Using the example above, this will be my book.
:ucβ yields the substituted value in all upper case.
Using the example above, this will be MY BOOK.
:icβ yields the substituted value with initial caps, e.g. the first letter of each word is capitalized.
Using the example above, this will be My Book. Indeed, no difference, since the value is already correctly cased.
To enforce lower case before applying initial case, use format modifiers :lc:ic.
:scβ yields the substituted value with an initial cap and the rest lower case.
Using the example above, this will be My Book. Again, no difference, since the value already has an initial capital.
To enforce lower case before applying initial case, use format modifiers :lc:sc. Now the result will be My book.
:lpad("N")and:lpad("N","S")β pads the value by repeatedly prepending the string S until the total width is N.
If S is omitted, uses spaces.
:rpad("N")and:rpad("N","S")β pads the value by repeatedly appending the string S until the total width is N.
If S is omitted, uses spaces.
:replace("SRC","DST")β replaces all occurrences of STR by DST,
If S is omitted, uses spaces.
:%fmt β apply standardprintf()formatting, e.g.%{key:%03d}yields the numeric value ofkeyas a 3-digit string, adding leading zeroes if necessary.
Careful bestie: when you're combining formatting and conditional interpolation, you have to check for the formatted value:
"This takes %{days:%02d=01|%{} day|%{} days}"
And you can stop a colon from splitting formatters with a backslash:
%{title:replace( ,\:)}
π The Control Hash π
The interpolation process needs two parameters: a hash with settings and values for the named arguments, and the string to be used as the template for interpolation. We're gonna call that hash the control hash.
The hash can have these keys:
argsβ this is either a hash containing replacement texts for the named variables, or a subroutine that gets called with a variable as its argument and returns a replacement value.
This element should be considered mandatory.
separatorβ the separator used to concatenate list values, see "List Values" above.
It defaults to Perl variable $" which, on its turn, defaults to a single space.
activatorβ a single character that activates interpolation. By default this is the percent%character.keypatternβ the pattern to match key names. Default isqr/\w+[-_\w.]*/.maxiterβ to enable nested substitutions and recursive replacement, the interpolation process is repeated until there are no more interpolations to be made. The maximun number of iterations is limited to the value ofmaxiter.
By default maxiter is 16.
An example of a control hash:
my %ctl =
( args => {
customer => [ "Jones", "Smith" ],
days => 2,
title => "My Title",
},
separator => ", ",
);
π«Ά Object Oriented API π«Ά
my $ii = String::Interpolate::Named->new;
$ii->ctl(\%ctl);
$result = $ii->interpolate($template);
And for convenience, the control hash may be passed to the constructor:
my $ii = String::Interpolate::Named->new(\%ctl);
$result = $ii->interpolate($template);
β Functional API β
String::Interpolate::Named provides a single function, interpolate, and it's exported by default. Obsessed.
The subroutine takes two arguments: a reference to a control hash, and the template string.
$result = interpolate( \%ctl, $template );
π METHODS π
π new π
Constructs a new String::Interpolate::Named object.
my $ii = String::Interpolate::Named->new;
or
my $ii = String::Interpolate::Named->new(\%ctl);
π ctl π
Associates a control hash with an existing object.
$ii->ctl(\%ctl);
π interpolate π
This routine performs the actual interpolations. It can be used as a method:
$ii->interpolate($template);
and functionally:
interpolate( \%ctl, $template );
π REQUIREMENTS π
Minimal Perl version 5.10.1. That's the floor, bestie.
π AUTHOR π
Johan Vromans, "<JV at CPAN dot org>" β the actual main character who built this.
π SUPPORT π
Development 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. They wanna hear from you fr fr. π
π¦ ACKNOWLEDGEMENTS π¦
Many of the existing template / interpolate / substitute modules. Respect where it's due. π
π₯ COPYRIGHT & LICENSE π₯
Copyright 2018,2025 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.
And that's the whole page, bestie π
β this lil' writeup came from perl v5.40.1, dated 2025-08-15, and it's the perl manual page String...:Named(3pm). Go interpolate something gorgeous. π«Άβ¨
π¦ where this page comes from
- ubuntu resolute: libstring-interpolate-named-perl 1.06-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libstring-interpolate-named-perl 1.06-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms