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} >>.
68 C<undef> is returned when the expression would have autovivified.
74 Turn off autovivification for dereferencing expressions that are parts of an C<exists>, such as C<< exists $hashref->{key}[$idx]{$field} >>.
75 C<''> is returned when the expression would have autovivified.
81 Turn off autovivification for dereferencing expressions that are parts of a C<delete>, such as C<< delete $hashref->{key}[$idx]{$field} >>.
82 C<undef> is returned when the expression would have autovivified.
88 Turn off autovivification for lvalue dereferencing expressions, such as C<< $hashref->{key}[$idx]{$field} = $value >>.
89 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).
95 Emit a warning when an autovivification is avoided.
101 Throw an exception when an autovivification is avoided.
105 Each call to C<unimport> adds the specified features to the ones already in use in the current lexical scope.
107 When C<@opts> is empty, it defaults to C<qw/fetch exists delete/>.
112 strict => A_HINT_STRICT,
114 fetch => A_HINT_FETCH,
115 store => A_HINT_STORE,
116 exists => A_HINT_EXISTS,
117 delete => A_HINT_DELETE,
122 my $hint = _detag($^H{+(__PACKAGE__)}) || 0;
123 @_ = qw/fetch exists delete/ unless @_;
124 $hint |= $bits{$_} for grep exists $bits{$_}, @_;
126 $^H{+(__PACKAGE__)} = _tag($hint);
130 =head2 C<import @opts>
132 Magically called when C<use autovivification> is encountered.
133 Disables the features given in C<@opts>, which can be the same as for L</unimport>.
135 Each call to C<import> removes the specified features to the ones already in use in the current lexical scope.
137 When C<@opts> is empty, it defaults to restoring the original Perl autovivification behaviour.
145 $hint = _detag($^H{+(__PACKAGE__)}) || 0;
146 $hint &= ~$bits{$_} for grep exists $bits{$_}, @_;
149 $^H{+(__PACKAGE__)} = _tag($hint);
157 L<XSLoader> (standard since perl 5.006).
165 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
167 You can contact me by mail or on C<irc.perl.org> (vincent).
171 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>.
172 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
176 You can find documentation for this module with the perldoc command.
178 perldoc autovivification
180 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/autovivification>.
182 =head1 ACKNOWLEDGEMENTS
184 Matt S. Trout asked for it.
186 =head1 COPYRIGHT & LICENSE
188 Copyright 2009 Vincent Pit, all rights reserved.
190 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
194 1; # End of autovivification