]> git.vpit.fr Git - perl/modules/autovivification.git/blob - README
This is 0.06
[perl/modules/autovivification.git] / README
1 NAME
2     autovivification - Lexically disable autovivification.
3
4 VERSION
5     Version 0.06
6
7 SYNOPSIS
8         no autovivification;
9
10         my $hashref;
11
12         my $a = $hashref->{key_a};       # $hashref stays undef
13
14         if (exists $hashref->{option}) { # Still undef
15          ...
16         }
17
18         delete $hashref->{old};          # Still undef again
19
20         $hashref->{new} = $value;        # Vivifies to { new => $value }
21
22 DESCRIPTION
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".
31
32     This pragma lets you disable autovivification for some constructs and
33     optionally throws a warning or an error when it would have happened.
34
35 METHODS
36   "unimport @opts"
37     Magically called when "no autovivification" is encountered. Enables the
38     features given in @opts, which can be :
39
40     *   'fetch'
41
42         Turn off autovivification for rvalue dereferencing expressions, such
43         as "$value = $hashref->{key}[$idx]{$field}", "keys
44         %{$hashref->{key}}" or "values %{$hashref->{key}}". Starting from
45         perl 5.11, it also covers "keys" and "values" on array references.
46         When the expression would have autovivified, "undef" is returned for
47         a plain fetch, while "keys" and "values" return 0 in scalar context
48         and the empty list in list context.
49
50     *   'exists'
51
52         Turn off autovivification for dereferencing expressions that are
53         parts of an "exists", such as "exists
54         $hashref->{key}[$idx]{$field}". '' is returned when the expression
55         would have autovivified.
56
57     *   'delete'
58
59         Turn off autovivification for dereferencing expressions that are
60         parts of a "delete", such as "delete $hashref->{key}[$idx]{$field}".
61         "undef" is returned when the expression would have autovivified.
62
63     *   'store'
64
65         Turn off autovivification for lvalue dereferencing expressions, such
66         as "$hashref->{key}[$idx]{$field} = $value" or "for
67         ($hashref->{key}[$idx]{$field}) { ... }". An exception is thrown if
68         vivification is needed to store the value, which means that
69         effectively you can only assign to levels that are already defined
70         (in the example, this would require "$hashref->{key}[$idx]" to
71         already be a hash reference).
72
73     *   'warn'
74
75         Emit a warning when an autovivification is avoided.
76
77     *   'strict'
78
79         Throw an exception when an autovivification is avoided.
80
81     Each call to "unimport" adds the specified features to the ones already
82     in use in the current lexical scope.
83
84     When @opts is empty, it defaults to "qw/fetch exists delete/".
85
86   "import @opts"
87     Magically called when "use autovivification" is encountered. Disables
88     the features given in @opts, which can be the same as for "unimport".
89
90     Each call to "import" removes the specified features to the ones already
91     in use in the current lexical scope.
92
93     When @opts is empty, it defaults to restoring the original Perl
94     autovivification behaviour.
95
96 CONSTANTS
97   "A_THREADSAFE"
98     True iff the module could have been built with thread-safety features
99     enabled. This constant only has a meaning with your perl is threaded ;
100     otherwise, it'll always be false.
101
102   "A_FORKSAFE"
103     True iff this module could have been built with fork-safety features
104     enabled. This will always be true except on Windows where it's false for
105     perl 5.10.0 and below .
106
107 CAVEATS
108     The pragma doesn't apply when one dereferences the returned value of an
109     array or hash slice, as in "@array[$id]->{member}" or
110     @hash{$key}->{member}. This syntax is valid Perl, yet it's discouraged
111     as the slice is here useless since the dereferencing enforces scalar
112     context. If warnings are turned on, Perl will complain about one-element
113     slices.
114
115 DEPENDENCIES
116     perl 5.8.
117
118     XSLoader (standard since perl 5.006).
119
120 SEE ALSO
121     perlref.
122
123 AUTHOR
124     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
125
126     You can contact me by mail or on "irc.perl.org" (vincent).
127
128 BUGS
129     Please report any bugs or feature requests to "bug-autovivification at
130     rt.cpan.org", or through the web interface at
131     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I
132     will be notified, and then you'll automatically be notified of progress
133     on your bug as I make changes.
134
135 SUPPORT
136     You can find documentation for this module with the perldoc command.
137
138         perldoc autovivification
139
140     Tests code coverage report is available at
141     <http://www.profvince.com/perl/cover/autovivification>.
142
143 ACKNOWLEDGEMENTS
144     Matt S. Trout asked for it.
145
146 COPYRIGHT & LICENSE
147     Copyright 2009,2010 Vincent Pit, all rights reserved.
148
149     This program is free software; you can redistribute it and/or modify it
150     under the same terms as Perl itself.
151