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';
40 no autovivification 'strict';
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.
108 Throws an exception when an autovivification is avoided.
110 Each call to "unimport" adds the specified features to the ones already
111 in use in the current lexical scope.
113 When @opts is empty, it defaults to "qw<fetch exists delete>".
116 use autovivification; # default Perl behaviour
117 use autovivification qw<fetch store exists delete>;
119 Magically called when "use autovivification @opts" is encountered.
120 Disables the features given in @opts, which can be the same as for
123 Each call to "import" removes the specified features to the ones already
124 in use in the current lexical scope.
126 When @opts is empty, it defaults to restoring the original Perl
127 autovivification behaviour.
131 True if and only if the module could have been built with thread-safety
132 features enabled. This constant only has a meaning when your perl is
133 threaded, otherwise it will always be false.
136 True if and only if this module could have been built with fork-safety
137 features enabled. This constant will always be true, except on Windows
138 where it is false for perl 5.10.0 and below.
141 The pragma doesn't apply when one dereferences the returned value of an
142 array or hash slice, as in "@array[$id]->{member}" or
143 @hash{$key}->{member}. This syntax is valid Perl, yet it is discouraged
144 as the slice is here useless since the dereferencing enforces scalar
145 context. If warnings are turned on, Perl will complain about one-element
151 A C compiler. This module may happen to build with a C++ compiler as
152 well, but don't rely on it, as no guarantee is made in this regard.
154 XSLoader (standard since perl 5.6.0).
160 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
162 You can contact me by mail or on "irc.perl.org" (vincent).
165 Please report any bugs or feature requests to "bug-autovivification at
166 rt.cpan.org", or through the web interface at
167 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I
168 will be notified, and then you'll automatically be notified of progress
169 on your bug as I make changes.
172 You can find documentation for this module with the perldoc command.
174 perldoc autovivification
176 Tests code coverage report is available at
177 <http://www.profvince.com/perl/cover/autovivification>.
180 Matt S. Trout asked for it.
183 Copyright 2009,2010,2011,2012,2013 Vincent Pit, all rights reserved.
185 This program is free software; you can redistribute it and/or modify it
186 under the same terms as Perl itself.