-
-
25 Sep 2021 18:50:13 UTC
- Distribution: Mock-Data
- Module version: 0.03
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Repository
- Issues
- Testers (195 / 7 / 0)
- Kwalitee
Bus factor: 1- 75.40% Coverage
- License: perl_5
- Perl: v5.10.0
- Activity
24 month- Tools
- Download (55.79KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
NAME
Mock::Data::Template - Create a generator that plugs other templates into a string
SYNOPSIS
my $mock= Mock::Data->new( generators => { first_name => ['Alex','Pat'], last_name => ['Smith','Jones'], name => Mock::Data::Template->new("{first_name} {last_name}"), ten_words => "{join word count=10}", } );
DESCRIPTION
Mock::Data provides a convenient and simple templating system where
"{...}"
in the text gets replaced by the output of another generator. The contents of the curly braces can be a simple template name (which is found by name in the collection of generators of the currentMock::Data
) or it can include parameters, both positional and named.SYNTAX
# Call without parameters "literal text {template_name} literal text" # Call with positional parameters "literal text {template_name literal_param_1 literal_param_2} literal text" # Call with named parameters "literal text {template_name param5=literal_val} literal text" # Call with whitespace in parameter (hex escapes) "literal text {template_name two{#20}words} literal text" # Call with zero-length string parameter (prefix => "") "literal text {template_name prefix={}}" # Call with nested templates "{template1 text{#20}with{#20}{template2}{#20}embedded}"
CONSTRUCTOR
new
Mock::Data::Template->new($template); ...->new(template => $template);
This constructor only accepts one attribute,
template
, which will be immediately parsed to check for syntax errors. Note that references to other generators are not resolved until the template is executed, which may cause exceptions if generators of those names are not present in theMock::Data
instance.Instances of
Mock::Data::Template
do not hold references to theMock::Data
or anything in it, and may be shared freely.ATTRIBUTES
template
The template string that was passed to the constructor
METHODS
compile
Return a coderef that executes the generator.
generate
Evaluate the template on the current Mock::Data and return the string.
to_string
Templates stringify as
"template('original_text')"
parse_template
my $tree= Mock::Data::Template->parse_template("{a}{b}{c {d}}"); my $sub= Mock::Data::Template->parse_template("{a}{b}{c {d}}", { compile => 1 });
Class or instance method. This parses a template string, returning a scalar, or an arrayref of parts where scalars are literal strings and arrayrefs represent a call to another generator. Arrayrefs in the parameter list of the call to a generator represent templates, and arrayrefs within that represent call, and arrayrefs within that represent templates, and so on.
If the
compile
flag is given, this returns a coderef instead of an arrayref (but can still return a plain scalar). The coderef matches the API for generators, taking a reference to Mock::Data as the first parameter.AUTHOR
Michael Conrad <mike@nrdvana.net>
VERSION
version 0.03
COPYRIGHT AND LICENSE
This software is copyright (c) 2021 by Michael Conrad.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Module Install Instructions
To install Mock::Data, copy and paste the appropriate command in to your terminal.
cpanm Mock::Data
perl -MCPAN -e shell install Mock::Data
For more information on module installation, please visit the detailed CPAN module installation guide.