1 package autovivification;
10 autovivification - Lexically disable autovivification.
29 my $a = $hashref->{key_a}; # $hashref stays undef
31 if (exists $hashref->{option}) { # Still undef
35 delete $hashref->{old}; # Still undef again
37 $hashref->{new} = $value; # Vivifies to { new => $value }
41 When an undefined variable is dereferenced, it gets silently upgraded to an array or hash reference (depending of the type of the dereferencing).
42 This behaviour is called I<autovivification> and usually does what you mean (e.g. when you store a value) but it's sometimes unnatural or surprising because your variables gets populated behind your back.
43 This is especially true when several levels of dereferencing are involved, in which case all levels are vivified up to the last, or when it happens in intuitively read-only constructs like C<exists>.
45 This pragma lets you disable autovivification for some constructs and optionally throws a warning or an error when it would have happened.
51 XSLoader::load(__PACKAGE__, $VERSION);
56 =head2 C<unimport @opts>
58 Magically called when C<no autovivification @opts> is encountered.
59 Enables the features given in C<@opts>, which can be :
67 Turns off autovivification for rvalue dereferencing expressions, such as :
69 $value = $arrayref->[$idx]
70 $value = $hashref->{$key}
74 Starting from perl C<5.11>, it also covers C<keys> and C<values> on array references :
79 When the expression would have autovivified, C<undef> is returned for a plain fetch, while C<keys> and C<values> return C<0> in scalar context and the empty list in list context.
85 Turns off autovivification for dereferencing expressions that are parts of an C<exists>, such as :
87 exists $arrayref->[$idx]
88 exists $hashref->{$key}
90 C<''> is returned when the expression would have autovivified.
96 Turns off autovivification for dereferencing expressions that are parts of a C<delete>, such as :
98 delete $arrayref->[$idx]
99 delete $hashref->{$key}
101 C<undef> is returned when the expression would have autovivified.
107 Turns off autovivification for lvalue dereferencing expressions, such as :
109 $arrayref->[$idx] = $value
110 $hashref->{$key} = $value
111 for ($arrayref->[$idx]) { ... }
112 for ($hashref->{$key}) { ... }
113 function($arrayref->[$idx])
114 function($hashref->{$key})
116 An exception is thrown if vivification is needed to store the value, which means that effectively you can only assign to levels that are already defined.
117 In the example, this would require C<$arrayref> (resp. C<$hashref>) to already be an array (resp. hash) reference.
123 Emits a warning when an autovivification is avoided.
129 Throws an exception when an autovivification is avoided.
133 Each call to C<unimport> adds the specified features to the ones already in use in the current lexical scope.
135 When C<@opts> is empty, it defaults to C<< qw<fetch exists delete> >>.
140 strict => A_HINT_STRICT,
142 fetch => A_HINT_FETCH,
143 store => A_HINT_STORE,
144 exists => A_HINT_EXISTS,
145 delete => A_HINT_DELETE,
150 my $hint = _detag($^H{+(__PACKAGE__)}) || 0;
151 @_ = qw<fetch exists delete> unless @_;
152 $hint |= $bits{$_} for grep exists $bits{$_}, @_;
154 $^H{+(__PACKAGE__)} = _tag($hint);
158 =head2 C<import @opts>
160 Magically called when C<use autovivification @opts> is encountered.
161 Disables the features given in C<@opts>, which can be the same as for L</unimport>.
163 Each call to C<import> removes the specified features to the ones already in use in the current lexical scope.
165 When C<@opts> is empty, it defaults to restoring the original Perl autovivification behaviour.
173 $hint = _detag($^H{+(__PACKAGE__)}) || 0;
174 $hint &= ~$bits{$_} for grep exists $bits{$_}, @_;
177 $^H{+(__PACKAGE__)} = _tag($hint);
183 =head2 C<A_THREADSAFE>
185 True iff the module could have been built with thread-safety features enabled.
186 This constant only has a meaning with your perl is threaded ; otherwise, it'll always be false.
190 True iff this module could have been built with fork-safety features enabled.
191 This will always be true except on Windows where it's false for perl 5.10.0 and below.
195 The pragma doesn't apply when one dereferences the returned value of an array or hash slice, as in C<< @array[$id]->{member} >> or C<< @hash{$key}->{member} >>.
196 This syntax is valid Perl, yet it's discouraged as the slice is here useless since the dereferencing enforces scalar context.
197 If warnings are turned on, Perl will complain about one-element slices.
204 This module may happen to build with a C++ compiler as well, but don't rely on it, as no guarantee is made in this regard.
206 L<XSLoader> (standard since perl 5.006).
214 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
216 You can contact me by mail or on C<irc.perl.org> (vincent).
220 Please report any bugs or feature requests to C<bug-autovivification at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>.
221 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
225 You can find documentation for this module with the perldoc command.
227 perldoc autovivification
229 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/autovivification>.
231 =head1 ACKNOWLEDGEMENTS
233 Matt S. Trout asked for it.
235 =head1 COPYRIGHT & LICENSE
237 Copyright 2009,2010,2011 Vincent Pit, all rights reserved.
239 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
243 1; # End of autovivification