1autovivification(3)   User Contributed Perl Documentation  autovivification(3)
2
3
4

NAME

6       autovivification - Lexically disable autovivification.
7

VERSION

9       Version 0.09
10

SYNOPSIS

12           no autovivification;
13
14           my $hashref;
15
16           my $a = $hashref->{key_a};       # $hashref stays undef
17
18           if (exists $hashref->{option}) { # Still undef
19            ...
20           }
21
22           delete $hashref->{old};          # Still undef again
23
24           $hashref->{new} = $value;        # Vivifies to { new => $value }
25

DESCRIPTION

27       When an undefined variable is dereferenced, it gets silently upgraded
28       to an array or hash reference (depending of the type of the
29       dereferencing).  This behaviour is called autovivification and usually
30       does what you mean (e.g. when you store a value) but it's sometimes
31       unnatural or surprising because your variables gets populated behind
32       your back.  This is especially true when several levels of
33       dereferencing are involved, in which case all levels are vivified up to
34       the last, or when it happens in intuitively read-only constructs like
35       "exists".
36
37       This pragma lets you disable autovivification for some constructs and
38       optionally throws a warning or an error when it would have happened.
39

METHODS

41   "unimport @opts"
42       Magically called when "no autovivification @opts" is encountered.
43       Enables the features given in @opts, which can be :
44
45       ·   'fetch'
46
47           Turns off autovivification for rvalue dereferencing expressions,
48           such as :
49
50               $value = $arrayref->[$idx]
51               $value = $hashref->{$key}
52               keys %$hashref
53               values %$hashref
54
55           Starting from perl 5.11, it also covers "keys" and "values" on
56           array references :
57
58               keys @$arrayref
59               values @$arrayref
60
61           When the expression would have autovivified, "undef" is returned
62           for a plain fetch, while "keys" and "values" return 0 in scalar
63           context and the empty list in list context.
64
65       ·   'exists'
66
67           Turns off autovivification for dereferencing expressions that are
68           parts of an "exists", such as :
69
70               exists $arrayref->[$idx]
71               exists $hashref->{$key}
72
73           '' is returned when the expression would have autovivified.
74
75       ·   'delete'
76
77           Turns off autovivification for dereferencing expressions that are
78           parts of a "delete", such as :
79
80               delete $arrayref->[$idx]
81               delete $hashref->{$key}
82
83           "undef" is returned when the expression would have autovivified.
84
85       ·   'store'
86
87           Turns off autovivification for lvalue dereferencing expressions,
88           such as :
89
90               $arrayref->[$idx] = $value
91               $hashref->{$key} = $value
92               for ($arrayref->[$idx]) { ... }
93               for ($hashref->{$key}) { ... }
94               function($arrayref->[$idx])
95               function($hashref->{$key})
96
97           An exception is thrown if vivification is needed to store the
98           value, which means that effectively you can only assign to levels
99           that are already defined In the example, this would require
100           $arrayref (resp. $hashref) to already be an array (resp. hash)
101           reference.
102
103       ·   'warn'
104
105           Emits a warning when an autovivification is avoided.
106
107       ·   'strict'
108
109           Throws an exception when an autovivification is avoided.
110
111       Each call to "unimport" adds the specified features to the ones already
112       in use in the current lexical scope.
113
114       When @opts is empty, it defaults to "qw<fetch exists delete>".
115
116   "import @opts"
117       Magically called when "use autovivification @opts" is encountered.
118       Disables the features given in @opts, which can be the same as for
119       "unimport".
120
121       Each call to "import" removes the specified features to the ones
122       already in use in the current lexical scope.
123
124       When @opts is empty, it defaults to restoring the original Perl
125       autovivification behaviour.
126

CONSTANTS

128   "A_THREADSAFE"
129       True iff the module could have been built with thread-safety features
130       enabled.  This constant only has a meaning with your perl is threaded ;
131       otherwise, it'll always be false.
132
133   "A_FORKSAFE"
134       True iff this module could have been built with fork-safety features
135       enabled.  This will always be true except on Windows where it's false
136       for perl 5.10.0 and below .
137

CAVEATS

139       The pragma doesn't apply when one dereferences the returned value of an
140       array or hash slice, as in "@array[$id]->{member}" or
141       @hash{$key}->{member}.  This syntax is valid Perl, yet it's discouraged
142       as the slice is here useless since the dereferencing enforces scalar
143       context.  If warnings are turned on, Perl will complain about one-
144       element slices.
145

DEPENDENCIES

147       perl 5.8.3.
148
149       A C compiler.  This module may happen to build with a C++ compiler as
150       well, but don't rely on it, as no guarantee is made in this regard.
151
152       XSLoader (standard since perl 5.006).
153

SEE ALSO

155       perlref.
156

AUTHOR

158       Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
159
160       You can contact me by mail or on "irc.perl.org" (vincent).
161

BUGS

163       Please report any bugs or feature requests to "bug-autovivification at
164       rt.cpan.org", or through the web interface at
165       <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>.  I
166       will be notified, and then you'll automatically be notified of progress
167       on your bug as I make changes.
168

SUPPORT

170       You can find documentation for this module with the perldoc command.
171
172           perldoc autovivification
173
174       Tests code coverage report is available at
175       <http://www.profvince.com/perl/cover/autovivification>.
176

ACKNOWLEDGEMENTS

178       Matt S. Trout asked for it.
179
181       Copyright 2009,2010,2011 Vincent Pit, all rights reserved.
182
183       This program is free software; you can redistribute it and/or modify it
184       under the same terms as Perl itself.
185
186
187
188perl v5.12.3                      2011-01-05               autovivification(3)
Impressum