The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


Mock::Data::Generator - Utilities and optional base class for authoring generators


This package provides a set of utility methods for writing generators, and an optional abstract base class. (a Generator does not need to inherit from this class)


The most basic Mock::Data generator is a simple coderef of the form

  sub ( $mockdata, \%arguments, @arguments ) { ... }

which returns a literal data item, usually a scalar. A generator can also be any object which has a "generate" method. Using an object provides more flexibility to handle cases where a user wants to combine generators.



  my $data= $generator->generate($mockdata, \%named_params, @pos_params);

Like the coderef, this takes an instance of Mock::Data as the first non-self argument, followed by a hashref of named parameters, followed by arbitrary positional parameters after that.


  my $callable= $generator->compile(@default_params);

Return a plain coderef that most optimally performs the generation for the @default_params. This implementation just wraps $self->generate(@defaults) in a coderef. Subclasses may provide more useful optimization.

The returned coderef takes one argument, of a Mock::Data instance.


  my $new_generator= $generator->combine_generator( $peer );

The default way to combine two generators is to create a new generator that selects each child generator 50% of the time. For generators that define a collection of possible data, it may be preferred to merge the collections in a manner different than a plain 50% split. This method allows for that custom behavior.


A generator that wants to perform special behavior when the Mock::Data instance gets cloned can implement this method. I can't think of any reason a generator should ever need this, since the "generator_state" in Mock::Data gets cloned. Lack of the method indicates the generator doesn't need this feature.


Michael Conrad <>


version 0.03


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.