]> git.vpit.fr Git - perl/modules/autovivification.git/blob - lib/autovivification.pm
This is 0.06
[perl/modules/autovivification.git] / lib / autovivification.pm
1 package autovivification;
2
3 use 5.008;
4
5 use strict;
6 use warnings;
7
8 =head1 NAME
9
10 autovivification - Lexically disable autovivification.
11
12 =head1 VERSION
13
14 Version 0.06
15
16 =cut
17
18 our $VERSION;
19 BEGIN {
20  $VERSION = '0.06';
21 }
22
23 =head1 SYNOPSIS
24
25     no autovivification;
26
27     my $hashref;
28
29     my $a = $hashref->{key_a};       # $hashref stays undef
30
31     if (exists $hashref->{option}) { # Still undef
32      ...
33     }
34
35     delete $hashref->{old};          # Still undef again
36
37     $hashref->{new} = $value;        # Vivifies to { new => $value }
38
39 =head1 DESCRIPTION
40
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>.
44
45 This pragma lets you disable autovivification for some constructs and optionally throws a warning or an error when it would have happened.
46
47 =cut
48
49 BEGIN {
50  require XSLoader;
51  XSLoader::load(__PACKAGE__, $VERSION);
52 }
53
54 =head1 METHODS
55
56 =head2 C<unimport @opts>
57
58 Magically called when C<no autovivification> is encountered.
59 Enables the features given in C<@opts>, which can be :
60
61 =over 4
62
63 =item *
64
65 C<'fetch'>
66
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.
70
71 =item *
72
73 C<'exists'>
74
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.
77
78 =item *
79
80 C<'delete'>
81
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.
84
85 =item *
86
87 C<'store'>
88
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).
91
92 =item *
93
94 C<'warn'>
95
96 Emit a warning when an autovivification is avoided.
97
98 =item *
99
100 C<'strict'>
101
102 Throw an exception when an autovivification is avoided.
103
104 =back
105
106 Each call to C<unimport> adds the specified features to the ones already in use in the current lexical scope.
107
108 When C<@opts> is empty, it defaults to C<qw/fetch exists delete/>.
109
110 =cut
111
112 my %bits = (
113  strict => A_HINT_STRICT,
114  warn   => A_HINT_WARN,
115  fetch  => A_HINT_FETCH,
116  store  => A_HINT_STORE,
117  exists => A_HINT_EXISTS,
118  delete => A_HINT_DELETE,
119 );
120
121 sub unimport {
122  shift;
123  my $hint = _detag($^H{+(__PACKAGE__)}) || 0;
124  @_ = qw/fetch exists delete/ unless @_;
125  $hint |= $bits{$_} for grep exists $bits{$_}, @_;
126  $^H |= 0x00020000;
127  $^H{+(__PACKAGE__)} = _tag($hint);
128  ();
129 }
130
131 =head2 C<import @opts>
132
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>.
135
136 Each call to C<import> removes the specified features to the ones already in use in the current lexical scope.
137
138 When C<@opts> is empty, it defaults to restoring the original Perl autovivification behaviour.
139
140 =cut
141
142 sub import {
143  shift;
144  my $hint = 0;
145  if (@_) {
146   $hint = _detag($^H{+(__PACKAGE__)}) || 0;
147   $hint &= ~$bits{$_} for grep exists $bits{$_}, @_;
148  }
149  $^H |= 0x00020000;
150  $^H{+(__PACKAGE__)} = _tag($hint);
151  ();
152 }
153
154 =head1 CONSTANTS
155
156 =head2 C<A_THREADSAFE>
157
158 True iff the module could have been built with thread-safety features enabled.
159 This constant only has a meaning with your perl is threaded ; otherwise, it'll always be false.
160
161 =head2 C<A_FORKSAFE>
162
163 True iff this module could have been built with fork-safety features enabled.
164 This will always be true except on Windows where it's false for perl 5.10.0 and below .
165
166 =head1 CAVEATS
167
168 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} >>.
169 This syntax is valid Perl, yet it's discouraged as the slice is here useless since the dereferencing enforces scalar context.
170 If warnings are turned on, Perl will complain about one-element slices.
171
172 =head1 DEPENDENCIES
173
174 L<perl> 5.8.
175
176 L<XSLoader> (standard since perl 5.006).
177
178 =head1 SEE ALSO
179
180 L<perlref>.
181
182 =head1 AUTHOR
183
184 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
185
186 You can contact me by mail or on C<irc.perl.org> (vincent).
187
188 =head1 BUGS
189
190 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>.
191 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
192
193 =head1 SUPPORT
194
195 You can find documentation for this module with the perldoc command.
196
197     perldoc autovivification
198
199 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/autovivification>.
200
201 =head1 ACKNOWLEDGEMENTS
202
203 Matt S. Trout asked for it.
204
205 =head1 COPYRIGHT & LICENSE
206
207 Copyright 2009,2010 Vincent Pit, all rights reserved.
208
209 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
210
211 =cut
212
213 1; # End of autovivification