2 autovivification - Lexically disable autovivification.
12 my $a = $hashref->{key_a}; # $hashref stays undef
14 if (exists $hashref->{option}) { # Still undef
18 delete $hashref->{old}; # Still undef again
20 $hashref->{new} = $value; # Vivifies to { new => $value }
23 When an undefined variable is dereferenced, it gets silently upgraded to
24 an array or hash reference (depending of the type of the dereferencing).
25 This behaviour is called *autovivification* and usually does what you
26 mean (e.g. when you store a value) but it may be unnatural or surprising
27 because your variables gets populated behind your back. This is
28 especially true when several levels of dereferencing are involved, in
29 which case all levels are vivified up to the last, or when it happens in
30 intuitively read-only constructs like "exists".
32 This pragma lets you disable autovivification for some constructs and
33 optionally throws a warning or an error when it would have happened.
37 no autovivification; # defaults to qw<fetch exists delete>
38 no autovivification qw<fetch store exists delete>;
39 no autovivification warn => @categories;
40 no autovivification strict => @categories;
42 Magically called when "no autovivification @opts" is encountered.
43 Enables the features given in @opts, which can be :
47 Turns off autovivification for rvalue dereferencing expressions,
50 $value = $arrayref->[$idx]
51 $value = $hashref->{$key}
55 Starting from perl 5.11, it also covers "keys" and "values" on array
61 When the expression would have autovivified, "undef" is returned for
62 a plain fetch, while "keys" and "values" return 0 in scalar context
63 and the empty list in list context.
67 Turns off autovivification for dereferencing expressions that are
68 parts of an "exists", such as :
70 exists $arrayref->[$idx]
71 exists $hashref->{$key}
73 '' is returned when the expression would have autovivified.
77 Turns off autovivification for dereferencing expressions that are
78 parts of a "delete", such as :
80 delete $arrayref->[$idx]
81 delete $hashref->{$key}
83 "undef" is returned when the expression would have autovivified.
87 Turns off autovivification for lvalue dereferencing expressions,
90 $arrayref->[$idx] = $value
91 $hashref->{$key} = $value
92 for ($arrayref->[$idx]) { ... }
93 for ($hashref->{$key}) { ... }
94 function($arrayref->[$idx])
95 function($hashref->{$key})
97 An exception is thrown if vivification is needed to store the value,
98 which means that effectively you can only assign to levels that are
99 already defined. In the example, this would require $arrayref (resp.
100 $hashref) to already be an array (resp. hash) reference.
104 Emits a warning when an autovivification is avoided for the
105 categories specified in @opts.
107 Note that "no autovivification 'warn'" currently does nothing by
108 itself, in particular it does not make the default categories warn.
109 This behaviour may change in a future version of this pragma.
113 Throws an exception when an autovivification is avoided for the
114 categories specified in @opts.
116 Note that "no autovivification 'strict'" currently does nothing by
117 itself, in particular it does not make the default categories die.
118 This behaviour may change in a future version of this pragma.
120 Each call to "unimport" adds the specified features to the ones already
121 in use in the current lexical scope.
123 When @opts is empty, it defaults to "qw<fetch exists delete>".
126 use autovivification; # default Perl behaviour
127 use autovivification qw<fetch store exists delete>;
129 Magically called when "use autovivification @opts" is encountered.
130 Disables the features given in @opts, which can be the same as for
133 Each call to "import" removes the specified features to the ones already
134 in use in the current lexical scope.
136 When @opts is empty, it defaults to restoring the original Perl
137 autovivification behaviour.
141 True if and only if the module could have been built with thread-safety
142 features enabled. This constant only has a meaning when your perl is
143 threaded, otherwise it will always be false.
146 True if and only if this module could have been built with fork-safety
147 features enabled. This constant will always be true, except on Windows
148 where it is false for perl 5.10.0 and below.
151 Using this pragma will cause a slight global slowdown of any subsequent
152 compilation phase that happens anywere in your code - even outside of
153 the scope of use of "no autovivification" - which may become noticeable
154 if you rely heavily on numerous calls to "eval STRING".
156 The pragma doesn't apply when one dereferences the returned value of an
157 array or hash slice, as in "@array[$id]->{member}" or
158 @hash{$key}->{member}. This syntax is valid Perl, yet it is discouraged
159 as the slice is here useless since the dereferencing enforces scalar
160 context. If warnings are turned on, Perl will complain about one-element
163 Autovivifications that happen in code "eval"'d during the global
164 destruction phase of a spawned thread or pseudo-fork (the processes used
165 internally for the "fork" emulation on Windows) are not reported.
170 A C compiler. This module may happen to build with a C++ compiler as
171 well, but don't rely on it, as no guarantee is made in this regard.
173 XSLoader (standard since perl 5.6.0).
179 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
181 You can contact me by mail or on "irc.perl.org" (vincent).
184 Please report any bugs or feature requests to "bug-autovivification at
185 rt.cpan.org", or through the web interface at
186 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I
187 will be notified, and then you'll automatically be notified of progress
188 on your bug as I make changes.
191 You can find documentation for this module with the perldoc command.
193 perldoc autovivification
196 Matt S. Trout asked for it.
199 Copyright 2009,2010,2011,2012,2013,2014,2015,2017 Vincent Pit, all
202 This program is free software; you can redistribute it and/or modify it
203 under the same terms as Perl itself.