NAME
Mock::Data::Util - Exportable functions to assist with declaring mock data
SYNOPSIS
use Mock::Data qw/
uniform_set
weighted_set
inflate_template
coerce_generator
mock_data_subclass
charset
template
/;
DESCRIPTION
This module contains utility functions for Mock::Data. These functions can be imported from this utility module, or (more conveniently) from Mock::Data itself.
EXPORTS
Nothing is exported by default. The following functions are available:
uniform_set
$generator= uniform_set( @items )
Shortcut to create a Mock::Data::Set with uniform probability.
weighted_set
$generator= weighted_set( $item => $weight, ... )
Shortcut to "new_weighted" in Mock::Data::Set
charset
$generator= charset('A-Z');
Shortcut for "new" in Mock::Data::Charset, which takes a perl-regex-notation character set string, or list of attributes.
template
$generator= template($template_string);
Shortcut for calling "new" in Mock::Data::Template.
inflate_template
my $str_or_generator= inflate_template( $string );
This function takes a string and checks it for template substitutions, returning a Template generator if it is a valid template, and returning the string otherwise. It may also return a string if the template substitutions were just escape sequences for literal strings. Don't call inflate_template
again on the output, because the escape sequences such as "{#7B}"
will have been replaced by a literal "{"
.
coerce_generator
my $generator= coerce_generator($spec);
Returns a Mock::Data::Generator wrapping the argument. The following types are handled:
- Scalar
-
Returns a Mock::Data::Template that performs template substitution on the string.
- ARRAY ref
-
Returns a "uniform_set".
- HASH ref
-
Returns a "weighted_set".
- CODE ref
-
Returns a Mock::Data::GeneratorSub wrapping the coderef.
- Regexp ref
-
Returns a Mock::Data::Regex generator.
$obj->can('generate')
-
Any object which has a
generate
method is returned as-is.
mock_data_subclass
my $subclass= mock_data_subclass($class, @package_names);
my $reblessed= mock_data_subclass($object, @package_names);
This method can be called on a class or instance to create a new package which inherits from the original and all packages in the list. If called on an instance, it also re-blesses the instance to the new class. All redundant items are removed from the combined list. (such as where one of the classes already inherits from one of the others).
This does not check if $package_name
is loaded. That is the caller's responsibility.
AUTHOR
Michael Conrad <mike@nrdvana.net>
VERSION
version 0.04
COPYRIGHT AND LICENSE
This software is copyright (c) 2024 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.