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" is encountered. Enables the
38 features given in @opts, which can be :
42 Turn off autovivification for rvalue dereferencing expressions, such
43 as "$value = $hashref->{key}[$idx]{$field}". "undef" is returned
44 when the expression would have autovivified.
48 Turn off autovivification for dereferencing expressions that are
49 parts of an "exists", such as "exists
50 $hashref->{key}[$idx]{$field}". '' is returned when the expression
51 would have autovivified.
55 Turn off autovivification for dereferencing expressions that are
56 parts of a "delete", such as "delete $hashref->{key}[$idx]{$field}".
57 "undef" is returned when the expression would have autovivified.
61 Turn off autovivification for lvalue dereferencing expressions, such
62 as "$hashref->{key}[$idx]{$field} = $value". An exception is thrown
63 if vivification is needed to store the value, which means that
64 effectively you can only assign to levels that are already defined
65 (in the example, this would require "$hashref->{key}[$idx]" to
66 already be a hash reference).
70 Emit a warning when an autovivification is avoided.
74 Throw an exception when an autovivification is avoided.
76 Each call to "unimport" adds the specified features to the ones already
77 in use in the current lexical scope.
79 When @opts is empty, it defaults to "qw/fetch exists delete/".
82 Magically called when "use autovivification" is encountered. Disables
83 the features given in @opts, which can be the same as for "unimport".
85 Each call to "import" removes the specified features to the ones already
86 in use in the current lexical scope.
88 When @opts is empty, it defaults to restoring the original Perl
89 autovivification behaviour.
94 XSLoader (standard since perl 5.006).
100 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
102 You can contact me by mail or on "irc.perl.org" (vincent).
105 Please report any bugs or feature requests to "bug-autovivification at
106 rt.cpan.org", or through the web interface at
107 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I
108 will be notified, and then you'll automatically be notified of progress
109 on your bug as I make changes.
112 You can find documentation for this module with the perldoc command.
114 perldoc autovivification
116 Tests code coverage report is available at
117 <http://www.profvince.com/perl/cover/autovivification>.
120 Matt S. Trout asked for it.
123 Copyright 2009 Vincent Pit, all rights reserved.
125 This program is free software; you can redistribute it and/or modify it
126 under the same terms as Perl itself.