Data::Fake(3pm)

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

✨ NAME ✨

Data::Fake β€” bestie, this thing declaratively generates fake structured data for testing πŸ’– (it's a Data::Fake(3pm) page, from the User Contributed Perl Documentation corner of the universe, and it is so useful, iykyk)

🌸 VERSION 🌸

version 0.006

πŸŽ€ SYNOPSIS πŸŽ€

use Data::Fake qw/Core Names Text Dates/;

my $hero_generator = fake_hash(
    {
        name      => fake_name(),
        battlecry => fake_sentences(1),
        birthday  => fake_past_datetime("%Y-%m-%d"),
        friends   => fake_array( fake_int(2,4), fake_name() ),
        gender    => fake_pick(qw/Male Female Other/),
    }
);

my $hero = $hero_generator->();

### example output ###
# {
#    'name'      => 'Antonio Nicholas Preston'
#    'battlecry' => 'Est ipsum corrupti quia voluptatibus.',
#    'birthday'  => '2004-04-21',
#    'friends'   => [
#                     'Jaylin Lillianna Morgan',
#                     'Colin Noel Estes',
#                     'Justice Aron Hale',
#                     'Franco Zane Oneil'
#                   ],
#    'gender'    => 'Male',
# };

like... look at him. a whole fake hero. generated. obsessed 🫢

πŸ¦‹ DESCRIPTION πŸ¦‹

This module generates randomized, fake structured data using declarative syntax. no cap.

Data::Fake is built on higher-order programming principles. It provides users with "factory" functions, which create "generator" functions for specific pieces of data. factories that make generators?? it's giving inception but make it cute πŸ’…

Wherever randomized, fake data is desired, a generator code reference is used as a placeholder for the desired output. Each time the top-level generator is run, nested generators are recursively run to turn placeholders into the desired randomized data.

For example, the SYNOPSIS declares the desired form of a "hero" using the fake_hash factory function. The input is a hash-reference, with nested generators created as placeholders by the fake_name, fake_sentences, etc. factory functions:

my $hero_generator = fake_hash(
    {
        name      => fake_name(),
        battlecry => fake_sentences(1),
        birthday  => fake_past_datetime("%Y-%m-%d"),
        friends   => fake_array( fake_int(2,4), fake_name() ),
        gender    => fake_pick(qw/Male Female Other/),
    }
);

Every time $hero_generator is run, a new hash is generated based on the template and nested generators. main character energy honestly 🌟

πŸ’œ USAGE πŸ’œ

🌈 Loading the right submodules 🌈

Factory functions are exported by submodules, loosely grouped by topic:

  • Data::Fake::Core – general purpose generators: hashes, arrays, numeric values, string templates
  • Data::Fake::Company – company name, job title
  • Data::Fake::Dates – past and future dates
  • Data::Fake::Internet – domain names, email addresses
  • Data::Fake::Names – first and last names
  • Data::Fake::Text – characters, words, sentences, paragraphs

Submodules can be loaded directly, to control exactly which functions are exported, or they can be loaded by the Data::Fake import function like so:

use Data::Fake qw/Core Company Dates Names/;

In this case, the listed submodules are loaded and all their functions are imported. so you just name the vibes you want and they all show up. periodt ✨

πŸ“ Constructing the desired output πŸ“

Use the factory functions to construct a generator for your data. Start with the highest level structure using fake_hash or fake_array. Use other factory functions to create placeholders with specific data types.

For example, a hash generator with placeholders for phone numbers:

$hash_generator = fake_hash(
    {
        home => fake_digits("###-###-####"),
        work => fake_digits("###-###-####"),
        cell => fake_digits("###-###-####"),
    }
);

Or build them up piece by piece. For example, an array of 100 of the previous hash references:

$array_generator = fake_array( 100, $hash_generator );

Then generate the hundred instances, each with three fake phone numbers:

$aoh = $array_generator->();

See Data::Fake::Examples for ideas for how to use and combine generators. literally go look, it's a whole mood board πŸ’–

🧚 Using custom generators 🧚

Generators are just code references. You can use your own anywhere a Data::Fake generator could be used:

$generator = fake_hash(
    favorite_color => \&my_favorite_color_picker,
    number_squared => sub { ( int(rand(10)) + 1 ) ** 2 },
);

You can (and probably should) write your own factory functions for anything complex or that you'll use more than once. You can use Data::Fake generators as part of these functions.

use Data::Fake qw/Core/;

sub fake_squared_int {
    my $max_int = shift;
    my $prng = fake_int( 1, $max_int );
    return sub { $prng->() ** 2 };
}

$generator = fake_hash(
    number_squared => fake_squared_int(10),
);

πŸ’€ Caveats and special cases πŸ’€

Because many data structures are walked recursively looking for code-references to replace, circular references will cause an infinite loop. okay so this one is NOT cute β€” you will simply loop forever, bestie, and no one is coming to save you 😭

If you need a code references as part of your output data structure, you need to wrap it in a code reference.

$generator = fake_hash(
    a_function => sub { \&some_function },
);

yeah. a code ref inside a code ref. it's code refs all the way down, don't think about it too hard πŸ’…

πŸ‘‘ CONTRIBUTING πŸ‘‘

If you have ideas for additional generator functions and think they would be sensible additions to the main distribution, please open a support ticket to discuss it. To be included in the main distribution, additional generator functions should add few, if any, additional dependencies. (keep it lightweight, the deps stay tiny, we don't do bloat here 🌸)

If you would like to release your own distributions in the Data::Fake::* namespace, please follow the conventions of the existing modules:

  • factory function names start with fake_
  • export all factory functions by default
  • allow code-references where you would allow a sizing constant

⭐ SEE ALSO ⭐

  • Data::Faker – similar but object oriented; doesn't do structured data; always loads all plugins
  • Data::Random – generate several random types of data
  • Test::Sims – generator for libraries generating random data
  • Text::Lorem – just fake text

πŸ’– AUTHOR πŸ’–

David Golden <dagolden@cpan.org>

🫢 CONTRIBUTORS 🫢

  • ChloΓ© Kekoa <50083900+chloekek@users.noreply.github.com>
  • JosΓ© JoaquΓ­n Atria <jjatria@gmail.com>
  • Ricardo Signes <rjbs@users.noreply.github.com>
  • Stuart Skelton <stuarts@broadbean.com>

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 shipped as perl v5.38.2, dated 2024-07-24, and the manual page itself goes by Data::Fake(3pm). go forth and generate fake little guys. slay πŸ’…βœ¨

πŸ“¦ where this page comes from