1autovivification(3)   User Contributed Perl Documentation  autovivification(3)
2
3
4

NAME

6       autovivification - Lexically disable autovivification.
7

VERSION

9       Version 0.18
10

SYNOPSIS

12           no autovivification;
13
14           my $hashref;
15
16           my $a = $hashref->{key_a};       # $hashref stays undef
17
18           if (exists $hashref->{option}) { # Still undef
19            ...
20           }
21
22           delete $hashref->{old};          # Still undef again
23
24           $hashref->{new} = $value;        # Vivifies to { new => $value }
25

DESCRIPTION

27       When an undefined variable is dereferenced, it gets silently upgraded
28       to an array or hash reference (depending of the type of the
29       dereferencing).  This behaviour is called autovivification and usually
30       does what you mean (e.g. when you store a value) but it may be
31       unnatural or surprising because your variables gets populated behind
32       your back.  This is especially true when several levels of
33       dereferencing are involved, in which case all levels are vivified up to
34       the last, or when it happens in intuitively read-only constructs like
35       "exists".
36
37       This pragma lets you disable autovivification for some constructs and
38       optionally throws a warning or an error when it would have happened.
39

METHODS

41   "unimport"
42           no autovivification; # defaults to qw<fetch exists delete>
43           no autovivification qw<fetch store exists delete>;
44           no autovivification warn   => @categories;
45           no autovivification strict => @categories;
46
47       Magically called when "no autovivification @opts" is encountered.
48       Enables the features given in @opts, which can be :
49
50       •   'fetch'
51
52           Turns off autovivification for rvalue dereferencing expressions,
53           such as :
54
55               $value = $arrayref->[$idx]
56               $value = $hashref->{$key}
57               keys %$hashref
58               values %$hashref
59
60           Starting from perl 5.11, it also covers "keys" and "values" on
61           array references :
62
63               keys @$arrayref
64               values @$arrayref
65
66           When the expression would have autovivified, "undef" is returned
67           for a plain fetch, while "keys" and "values" return 0 in scalar
68           context and the empty list in list context.
69
70       •   'exists'
71
72           Turns off autovivification for dereferencing expressions that are
73           parts of an "exists", such as :
74
75               exists $arrayref->[$idx]
76               exists $hashref->{$key}
77
78           '' is returned when the expression would have autovivified.
79
80       •   'delete'
81
82           Turns off autovivification for dereferencing expressions that are
83           parts of a "delete", such as :
84
85               delete $arrayref->[$idx]
86               delete $hashref->{$key}
87
88           "undef" is returned when the expression would have autovivified.
89
90       •   'store'
91
92           Turns off autovivification for lvalue dereferencing expressions,
93           such as :
94
95               $arrayref->[$idx] = $value
96               $hashref->{$key} = $value
97               for ($arrayref->[$idx]) { ... }
98               for ($hashref->{$key}) { ... }
99               function($arrayref->[$idx])
100               function($hashref->{$key})
101
102           An exception is thrown if vivification is needed to store the
103           value, which means that effectively you can only assign to levels
104           that are already defined.  In the example, this would require
105           $arrayref (resp. $hashref) to already be an array (resp. hash)
106           reference.
107
108       •   'warn'
109
110           Emits a warning when an autovivification is avoided for the
111           categories specified in @opts.
112
113           Note that "no autovivification 'warn'" currently does nothing by
114           itself, in particular it does not make the default categories warn.
115           This behaviour may change in a future version of this pragma.
116
117       •   'strict'
118
119           Throws an exception when an autovivification is avoided for the
120           categories specified in @opts.
121
122           Note that "no autovivification 'strict'" currently does nothing by
123           itself, in particular it does not make the default categories die.
124           This behaviour may change in a future version of this pragma.
125
126       Each call to "unimport" adds the specified features to the ones already
127       in use in the current lexical scope.
128
129       When @opts is empty, it defaults to "qw<fetch exists delete>".
130
131   "import"
132           use autovivification; # default Perl behaviour
133           use autovivification qw<fetch store exists delete>;
134
135       Magically called when "use autovivification @opts" is encountered.
136       Disables the features given in @opts, which can be the same as for
137       "unimport".
138
139       Each call to "import" removes the specified features to the ones
140       already in use in the current lexical scope.
141
142       When @opts is empty, it defaults to restoring the original Perl
143       autovivification behaviour.
144

CONSTANTS

146   "A_THREADSAFE"
147       True if and only if the module could have been built with thread-safety
148       features enabled.  This constant only has a meaning when your perl is
149       threaded, otherwise it will always be false.
150
151   "A_FORKSAFE"
152       True if and only if this module could have been built with fork-safety
153       features enabled.  This constant will always be true, except on Windows
154       where it is false for perl 5.10.0 and below.
155

CAVEATS

157       Using this pragma will cause a slight global slowdown of any subsequent
158       compilation phase that happens anywere in your code - even outside of
159       the scope of use of "no autovivification" - which may become noticeable
160       if you rely heavily on numerous calls to "eval STRING".
161
162       The pragma doesn't apply when one dereferences the returned value of an
163       array or hash slice, as in "@array[$id]->{member}" or
164       "@hash{$key}->{member}".  This syntax is valid Perl, yet it is
165       discouraged as the slice is here useless since the dereferencing
166       enforces scalar context.  If warnings are turned on, Perl will complain
167       about one-element slices.
168
169       Autovivifications that happen in code "eval"'d during the global
170       destruction phase of a spawned thread or pseudo-fork (the processes
171       used internally for the "fork" emulation on Windows) are not reported.
172

DEPENDENCIES

174       perl 5.8.3.
175
176       A C compiler.  This module may happen to build with a C++ compiler as
177       well, but don't rely on it, as no guarantee is made in this regard.
178
179       XSLoader (standard since perl 5.6.0).
180

SEE ALSO

182       perlref.
183

AUTHOR

185       Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
186
187       You can contact me by mail or on "irc.perl.org" (vincent).
188

BUGS

190       Please report any bugs or feature requests to "bug-autovivification at
191       rt.cpan.org", or through the web interface at
192       <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>.  I
193       will be notified, and then you'll automatically be notified of progress
194       on your bug as I make changes.
195

SUPPORT

197       You can find documentation for this module with the perldoc command.
198
199           perldoc autovivification
200

ACKNOWLEDGEMENTS

202       Matt S. Trout asked for it.
203
205       Copyright 2009,2010,2011,2012,2013,2014,2015,2017 Vincent Pit, all
206       rights reserved.
207
208       This program is free software; you can redistribute it and/or modify it
209       under the same terms as Perl itself.
210
211
212
213perl v5.38.0                      2023-07-21               autovivification(3)
Impressum