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> is encountered.
59 Enables the features given in C<@opts>, which can be :
67 Turn off autovivification for rvalue dereferencing expressions, such as C<< $value = $hashref->{key}[$idx]{$field} >>, C<< keys %{$hashref->{key}} >> or C<< values %{$hashref->{key}} >>.
68 Starting from perl C<5.11>, it also covers C<keys> and C<values> on array references.
69 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.
75 Turn off autovivification for dereferencing expressions that are parts of an C<exists>, such as C<< exists $hashref->{key}[$idx]{$field} >>.
76 C<''> is returned when the expression would have autovivified.
82 Turn off autovivification for dereferencing expressions that are parts of a C<delete>, such as C<< delete $hashref->{key}[$idx]{$field} >>.
83 C<undef> is returned when the expression would have autovivified.
89 Turn off autovivification for lvalue dereferencing expressions, such as C<< $hashref->{key}[$idx]{$field} = $value >> or C<< for ($hashref->{key}[$idx]{$field}) { ... } >>.
90 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 (in the example, this would require C<< $hashref->{key}[$idx] >> to already be a hash reference).
96 Emit a warning when an autovivification is avoided.
102 Throw an exception when an autovivification is avoided.
106 Each call to C<unimport> adds the specified features to the ones already in use in the current lexical scope.
108 When C<@opts> is empty, it defaults to C<qw/fetch exists delete/>.
113 strict => A_HINT_STRICT,
115 fetch => A_HINT_FETCH,
116 store => A_HINT_STORE,
117 exists => A_HINT_EXISTS,
118 delete => A_HINT_DELETE,
123 my $hint = _detag($^H{+(__PACKAGE__)}) || 0;
124 @_ = qw/fetch exists delete/ unless @_;
125 $hint |= $bits{$_} for grep exists $bits{$_}, @_;
127 $^H{+(__PACKAGE__)} = _tag($hint);
131 =head2 C<import @opts>
133 Magically called when C<use autovivification> is encountered.
134 Disables the features given in C<@opts>, which can be the same as for L</unimport>.
136 Each call to C<import> removes the specified features to the ones already in use in the current lexical scope.
138 When C<@opts> is empty, it defaults to restoring the original Perl autovivification behaviour.
146 $hint = _detag($^H{+(__PACKAGE__)}) || 0;
147 $hint &= ~$bits{$_} for grep exists $bits{$_}, @_;
150 $^H{+(__PACKAGE__)} = _tag($hint);
156 =head2 C<A_THREADSAFE>
158 True iff the module could have been built with thread-safety features enabled.
162 True iff this module could have been built with fork-safety features enabled.
163 This will always be true except on Windows where it's false for perl 5.10.0 and below .
167 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} >>.
168 This syntax is valid Perl, yet it's discouraged as the slice is here useless since the dereferencing enforces scalar context.
169 If warnings are turned on, Perl will complain about one-element slices.
175 L<XSLoader> (standard since perl 5.006).
183 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
185 You can contact me by mail or on C<irc.perl.org> (vincent).
189 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>.
190 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
194 You can find documentation for this module with the perldoc command.
196 perldoc autovivification
198 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/autovivification>.
200 =head1 ACKNOWLEDGEMENTS
202 Matt S. Trout asked for it.
204 =head1 COPYRIGHT & LICENSE
206 Copyright 2009,2010 Vincent Pit, all rights reserved.
208 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
212 1; # End of autovivification