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's sometimes unnatural or
27 surprising because your variables gets populated behind your back. This
28 is 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 Magically called when "no autovivification @opts" is encountered.
38 Enables the features given in @opts, which can be :
42 Turns off autovivification for rvalue dereferencing expressions,
45 $value = $arrayref->[$idx]
46 $value = $hashref->{$key}
50 Starting from perl 5.11, it also covers "keys" and "values" on array
56 When the expression would have autovivified, "undef" is returned for
57 a plain fetch, while "keys" and "values" return 0 in scalar context
58 and the empty list in list context.
62 Turns off autovivification for dereferencing expressions that are
63 parts of an "exists", such as :
65 exists $arrayref->[$idx]
66 exists $hashref->{$key}
68 '' is returned when the expression would have autovivified.
72 Turns off autovivification for dereferencing expressions that are
73 parts of a "delete", such as :
75 delete $arrayref->[$idx]
76 delete $hashref->{$key}
78 "undef" is returned when the expression would have autovivified.
82 Turns off autovivification for lvalue dereferencing expressions,
85 $arrayref->[$idx] = $value
86 $hashref->{$key} = $value
87 for ($arrayref->[$idx]) { ... }
88 for ($hashref->{$key}) { ... }
89 function($arrayref->[$idx])
90 function($hashref->{$key})
92 An exception is thrown if vivification is needed to store the value,
93 which means that effectively you can only assign to levels that are
94 already defined In the example, this would require $arrayref (resp.
95 $hashref) to already be an array (resp. hash) reference.
99 Emits a warning when an autovivification is avoided.
103 Throws an exception when an autovivification is avoided.
105 Each call to "unimport" adds the specified features to the ones already
106 in use in the current lexical scope.
108 When @opts is empty, it defaults to "qw/fetch exists delete/".
111 Magically called when "use autovivification @opts" is encountered.
112 Disables the features given in @opts, which can be the same as for
115 Each call to "import" removes the specified features to the ones already
116 in use in the current lexical scope.
118 When @opts is empty, it defaults to restoring the original Perl
119 autovivification behaviour.
123 True iff the module could have been built with thread-safety features
124 enabled. This constant only has a meaning with your perl is threaded ;
125 otherwise, it'll always be false.
128 True iff this module could have been built with fork-safety features
129 enabled. This will always be true except on Windows where it's false for
130 perl 5.10.0 and below .
133 The pragma doesn't apply when one dereferences the returned value of an
134 array or hash slice, as in "@array[$id]->{member}" or
135 @hash{$key}->{member}. This syntax is valid Perl, yet it's discouraged
136 as the slice is here useless since the dereferencing enforces scalar
137 context. If warnings are turned on, Perl will complain about one-element
143 A C compiler. This module may happen to build with a C++ compiler as
144 well, but don't rely on it, as no guarantee is made in this regard.
146 XSLoader (standard since perl 5.006).
152 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
154 You can contact me by mail or on "irc.perl.org" (vincent).
157 Please report any bugs or feature requests to "bug-autovivification at
158 rt.cpan.org", or through the web interface at
159 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I
160 will be notified, and then you'll automatically be notified of progress
161 on your bug as I make changes.
164 You can find documentation for this module with the perldoc command.
166 perldoc autovivification
168 Tests code coverage report is available at
169 <http://www.profvince.com/perl/cover/autovivification>.
172 Matt S. Trout asked for it.
175 Copyright 2009,2010 Vincent Pit, all rights reserved.
177 This program is free software; you can redistribute it and/or modify it
178 under the same terms as Perl itself.