MakeMe...Builder(3pm)

πŸ“¦
version 1.01 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

okay bestie, welcome to MakeMe...Builder(3pm) β€” that's the manual name, and this page is from the User Contributed Perl Documentation. It's all about Class::MakeMethods::Utility::TextBuilder, a one-function wonder that is literally here to do basic text substitutions. πŸŽ€

πŸ’– NAME πŸ’–

Class::MakeMethods::Utility::TextBuilder - Basic text substitutions

✨ SYNOPSIS ✨

print text_builder( $base_text, @exprs )

πŸ¦‹ DESCRIPTION πŸ¦‹

This module provides a single function. It implements a simple "text macro" mechanism for assembling templated text strings. slay.

$expanded_text = text_builder( $base_text, @exprs )

It returns a modified copy of $base_text using rules from the @exprs list. no cap.

The @exprs list may contain any of the following:

  • A string β€” any '*' characters will be replaced by the base text. The interpolated string then replaces the base text.
  • A code-ref β€” it will be called with the base text as its only argument. The result of that call then replaces the base text.
  • A hash-ref β€” it will be added to the substitution hash used in the second pass, below.
  • An array-ref β€” it contains additional expressions to be treated as above.

After any initial string and code-ref rules have been applied, the hash of substitution rules are applied.

The text will be searched for occurrences of the keys of the substitution hash. Those occurrences get modified based on the corresponding value in the hash. If the substitution key ends with '{}', the search will also match a balanced block of braces, and that value will also be used in the substitution.

The hash-ref may contain the following types of rules:

  • 'string' => 'string' β€” occurrences of the first string are to be replaced by the second.
  • 'string' => code_ref β€” occurrences of the string are to be replaced by the results of calling the subroutine with no arguments.
  • 'string{}' => 'string' β€” occurrences of the first string and subsequent block of braces are replaced by a copy of the second string in which any '*' characters have first been replaced by the contents of the brace block.
  • 'string{}' => code_ref β€” occurrences of the string and subsequent block of braces are replaced by the results of calling the subroutine with the contents of the brace block as its only argument.
  • 'string{}' => hash_ref β€” occurrences of the string and subsequent block of braces are replaced by using the contents of the brace block as a key into the provided hash-ref.

πŸŽ€ EXAMPLE πŸŽ€

These text strings and modification rules give you a skeleton for a collection letter:

my $letter = "You owe us AMOUNT. Please pay up!\n\n" .
                 "THREAT{SEVERITY}";

my @exprs = (
  "Dear NAMEm\n\n*",
  "*\n\n-- The Management",

  { 'THREAT{}' => { 'good'=>'Please?', 'bad'=>'Or else!' } },

  "\t\t\t\tDATE\n*",
  { 'DATE' => 'Tuesday, April 1, 2001' },
);

You'd invoke this template by giving it additional data for a given instance and calling the text_builder function:

my $item = { 'NAME'=>'John', 'AMOUNT'=>'200 camels', 'SEVERITY'=>'bad' };

print text_builder( $letter, @exprs, $item );

The resulting output is shown below:

                                         Tuesday, April 1, 2001
         Dear John,

         You owe us 200 camels. Please pay up!

         Or else!

         -- The Management

🌈 SEE ALSO 🌈

See Class::MakeMethods for general information about this distribution.


that's the vibe check, bestie πŸ’œ β€” this page is from perl v5.36.0, dated 2022-10-13, and it's filed under MakeMe...Builder(3pm). mwah! 🫢

πŸ“¦ where this page comes from