1Data::Faker(3) User Contributed Perl Documentation Data::Faker(3)
2
3
4
6 Data::Faker - Perl extension for generating fake data
7
9 use Data::Faker;
10
11 my $faker = Data::Faker->new();
12
13 print "Name: ".$faker->name."\n";
14 print "Company: ".$faker->company."\n";
15 print "Address: ".$faker->street_address."\n";
16 print " ".$faker->city.", ".$faker->us_state_abbr." ".$faker->us_zip_code."\n";
17
19 This module creates fake (but reasonable) data that can be used for
20 things such as filling databases with fake information during
21 development of database related applications.
22
24 new()
25 Object constructor. As a shortcut, you can pass names of plugin
26 modules to load to new(), although this does not actually restrict
27 the functions available to the object, it just causes those plugins
28 to be loaded if they haven't been loaded already. All Data::Faker
29 objects in one interpreter share the plugin data, so that multiple
30 objects don't multiply the memory requirements.
31
32 methods();
33 Return a list of the methods that have been provided by all of the
34 loaded plugins.
35
36 register_plugin();
37 Plugin modules call register_plugin() to provide data methods. See
38 any of the included plugin modules for examples.
39
41 You can specify which plugins to load by including just the base part
42 of their name as an argument when loading the module with 'use'. For
43 example if you only wanted to use data from the Data::Faker::Name
44 module, you would load Data::Faker like this:
45
46 use Data::Faker qw(Name);
47
48 By default any modules matching Data::Faker::* in any directory in @INC
49 will be loaded. You can also pass plugin names when calling the new()
50 method, and they will be loaded if not already in memory. See new().
51
53 Writing a plugin to provide new kinds of data is easy, all you have to
54 do is create a module named Data::Faker::SomeModuleName that inherits
55 from Data::Faker.
56
57 To provide data, the plugin merely needs to call the register_plugin
58 function with one or more pairs of function name and function data,
59 like this:
60
61 #!/usr/bin/perl -w
62 use strict;
63 use warnings;
64 use Data::Faker;
65
66 my $faker = Data::Faker->new();
67 print "My fake data is ".$faker->some_data_function."\n";
68
69 package Data::Faker::SomeData;
70 use base 'Data::Faker';
71
72 __PACKAGE__->register_plugin(
73 some_data_function => [qw(foo bar baz gazonk)],
74 another_data_item => sub { return '$some_data_function' },
75 );
76
77 The first argument is the method that will be made available to your
78 object, the second is a data source. If the data source is not a
79 reference, it will simply be returned as the data, if it is a reference
80 to an array, a random element from the array will be returned, and if
81 it is a subroutine reference, the subroutine will be run and the
82 results will be returned. The data that your data source provides is
83 checked for two things, tokens (that look like perl variables, starting
84 with a $), and numeric indicators (#). Any tokens found will be
85 replaced with their values, and any numeric indicators will be replaced
86 with random numbers. You can include a literal $ or # by prefacing it
87 with a backslash. If you load more than one module that defines the
88 same function, it has an additive effect, when the function is called
89 one of the data sources provided will be selected at random and then it
90 will be called to get a piece of data.
91
92 Some data source examples:
93
94 __PACKAGE__->register_plugin(
95 age => ['#','##'],
96 monetary_amount => ['\$####.##','\$###.##', '\$##.##', '\$#.##'],
97 adult_age => sub { int(rand(70)+18) },
98 );
99
100 If your data source is a code reference, it will receive the calling
101 object as an argument so you can build data out of other data if you
102 need to. See Data::Faker::DateTime for some examples of this.
103
105 There is no way to selectively remove data sources from a plugin that
106 was loaded, even if you didn't load it.
107
109 Text::Lorem
110
112 Jason Kohles, <email@jasonkohles.com>
113
115 Copyright 2004-2005 by Jason Kohles
116
117 This library is free software; you can redistribute it and/or modify it
118 under the same terms as Perl itself.
119
120
121
122perl v5.36.0 2023-01-20 Data::Faker(3)