Data::Fake::Core(3pm)

πŸ“¦
version 0.006 Β· 3 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ NAME ✨

okay so bestie, meet Data::Fake::Core β€” general purpose generators πŸ’– (and yeah, it's chillin' in the User Contributed Perl Documentation, iykyk)


πŸŽ€ VERSION πŸŽ€

version 0.006 β€” she's babygirl, she's iconic, she's current


πŸ¦‹ SYNOPSIS πŸ¦‹

use Data::Fake::Core;

$generator = fake_hash(
    {
        ssn             => fake_digits("###-##-###"),
        phrase          => fake_template(
                            "%s world", fake_pick(qw/hello goodbye/)
                           ),
        die_rolls       => fake_array( 3, fake_int(1, 6) ),
        temperature     => fake_float(-20.0, 120.0),
    }
);

🌸 DESCRIPTION 🌸

This module hands you a general-purpose lil' set of fake data functions, and the way it serves is: structured data, numeric data, structured strings, and weighted alternatives. all of it. πŸ’…

and the best part? All functions are exported by default. no extra asking, no begging, they just show up. periodt.


🧚 FUNCTIONS 🧚

πŸ‘‘ fake_hash

$generator = fake_hash(
    {
        name => fake_name,
        pet => fake_pick(qw/dog cat frog/),
    }
);

$generator = fake_hash( @hash_or_hash_generators );

so fake_hash gives you back a code reference, and when you run that code reference, boom β€” it generates a hash reference. main character behaviour fr.

the simplest way to use her: hand her a hash reference where some of the values are fake_* generator functions instead of boring normal values. when the generator runs, the hash gets walked recursively and every code reference it finds gets swapped out for its output. slay.

but if you pass more than one argument, then when the generator runs, they get merged according to these rules:

  • code references will be replaced with their outputs
  • after replacement, if any arguments aren't hash references, an exception will be thrown 😭
  • hash references will be shallow-merged

this merging thing is lowkey powerful β€” it lets you generate sections of hashes differently, or generate hashes that have missing keys (e.g. using fake_binomial):

# 25% of the time, generate a hash with a 'spouse' key
$factory = fake_hash(
    { ... },
    fake_binomial( 0.25, { spouse => fake_name() }, {} ),
);

🫢 fake_array

$generator = fake_array( 5, fake_digits("###-###-####") );

fake_array takes a positive integer size and a source argument, and returns a generator that returns an array reference where every element is built from the source. cute, consistent, obsessed.

and if the size is a code reference? then it gets run, and it can set a different size for every single array generated. variety, she's everything:

# arrays from size 1 to size 6
$generator = fake_array( fake_int(1,6), fake_digits("###-###-###") );

if the source is a code reference, it will be run; and if the source is a hash or array reference, it will be recursively evaluated just like fake_hash.

πŸ’– fake_pick

$generator = fake_pick( qw/one two three/ );
$generator = fake_pick( @generators );

give her literal values or code references, and she returns a generator that randomly picks one of them β€” all with equal probability, no favourites, no drama. and if the choice is a code reference, it will be run; if the choice is a hash or array reference, it gets recursively evaluated the way fake_hash or fake_array would do. 🌈

πŸ’€ fake_binomial

$generator = fake_binomial(
    0.90,
    { name => fake_name() }, # 90% likely
    {},                      # 10% likely
);

$generator = fake_binomial( $prob, $lte_outcome, $gt_outcome );

fake_binomial takes a probability and two outcomes. the probability (between 0 and 1.0) is how likely it is that the return value will be the first outcome. the rest of the time, the return value will be the second outcome. it's giving coin flip with commitment issues. πŸͺ™

and yeah β€” if the outcome is a code reference, it will be run; if the outcome is a hash or array reference, it will be recursively evaluated like fake_hash or fake_array would do.

⭐ fake_weighted

$generator = fake_weighted(
    [ 'a_choice',          1 ],
    [ 'ten_times_likely', 10 ],
    [ $another_generator,  1 ],
);

you give her a list of array references, and each one holds a value plus a non-negative weight. she returns a generator that randomly selects a value according to those relative weights. so ten_times_likely really is out here being ten times more likely. not me doing the math. πŸ™ˆ

if the value is a code reference, it will be run; and if it's a hash or array reference, it gets recursively evaluated like fake_hash or fake_array would do.

πŸ”₯ fake_int

$generator = fake_int(1, 6);

give her a minimum and a maximum value, and she returns a generator that will produce a random integer in that range. that's it, that's the whole vibe.

🌊 fake_float

$generator = fake_float(1.0, 6.0);

give her a minimum and a maximum value, and she returns a generator that will produce a random floating point value in that range. floaty and free. πŸ’œ

πŸ“ fake_digits

$generator = fake_digits('###-####'); # "555-1234"
$generator = fake_digits('\###');     # "#12"

give her a text pattern, and she returns a generator that replaces all occurrences of the sharp character (#) with a randomly selected digit. 🎲

want an actual literal sharp character and not a digit? escape it with a backslash. (and do it in a single-quoted string so you don't have to double your backslash to get a backslash in the string. yes really.)

use this for phone numbers, currencies, or literally whatever else needs random digits:

fake_digits('###-##-####');     # US Social Security Number
fake_digits('(###) ###-####');  # (800) 555-1212

🧁 fake_template

$generator = fake_template("Hello, %s", fake_name());

give her a sprintf-style text pattern plus a list of generators, and she returns a generator that, when run, executes the generators and returns the string populated with the output. πŸ’«

use this for creating custom generators out of other generators. building your own little empire, we love to see it.

πŸŽ€ fake_join

$generator = fake_join(" ", fake_first_name(), fake_surname() );

give her a character to join on, plus a list of literals or generators, and she returns a generator that, when run, executes any generators and returns them all concatenated together, separated by that separator character. group chat energy, honestly.

and the separator itself can be a generator too, if you want that degree of randomness as well:

$generator = fake_join( fake_pick( q{}, q{ }, q{,} ), @args );

πŸ¦‹ fake_flatten

$flatten_generator = fake_flatten( fake_array( 3, fake_first_name() ) );
@array_of_names = $flatten_generator->();

give her a generator that returns an array ref (like fake_array) or a hash ref (fake_hash), and fake_flatten returns a generator that, when run, executes the generators and returns their result in a dereferenced state.

this is especially useful when the return value is used directly as input to another function β€” for example, inside a fake_join:

$generator = fake_join( " ", $flatten_generator );

πŸ’… AUTHOR πŸ’…

David Golden <dagolden@cpan.org> β€” king behaviour, no cap


This software is Copyright (c) 2015 by David Golden.

This is free software, licensed under:

The Apache License, Version 2.0, January 2004

and that's the whole page bestie 🫢 β€” this was perl v5.38.2, dated 2024-07-24, and yeah, the manual name really is Data::Fake::Core(3pm). go forth and generate fake data responsibly. mwah πŸ’–βœ¨

πŸ“¦ where this page comes from