]> git.vpit.fr Git - perl/modules/autovivification.git/blob - lib/autovivification.pm
Don't autovivify in keys/values
[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.01
15
16 =cut
17
18 our $VERSION;
19 BEGIN {
20  $VERSION = '0.01';
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 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.
69
70 =item *
71
72 C<'exists'>
73
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.
76
77 =item *
78
79 C<'delete'>
80
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.
83
84 =item *
85
86 C<'store'>
87
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).
90
91 =item *
92
93 C<'warn'>
94
95 Emit a warning when an autovivification is avoided.
96
97 =item *
98
99 C<'strict'>
100
101 Throw an exception when an autovivification is avoided.
102
103 =back
104
105 Each call to C<unimport> adds the specified features to the ones already in use in the current lexical scope.
106
107 When C<@opts> is empty, it defaults to C<qw/fetch exists delete/>.
108
109 =cut
110
111 my %bits = (
112  strict => A_HINT_STRICT,
113  warn   => A_HINT_WARN,
114  fetch  => A_HINT_FETCH,
115  store  => A_HINT_STORE,
116  exists => A_HINT_EXISTS,
117  delete => A_HINT_DELETE,
118 );
119
120 sub unimport {
121  shift;
122  my $hint = _detag($^H{+(__PACKAGE__)}) || 0;
123  @_ = qw/fetch exists delete/ unless @_;
124  $hint |= $bits{$_} for grep exists $bits{$_}, @_;
125  $^H |= 0x00020000;
126  $^H{+(__PACKAGE__)} = _tag($hint);
127  ();
128 }
129
130 =head2 C<import @opts>
131
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>.
134
135 Each call to C<import> removes the specified features to the ones already in use in the current lexical scope.
136
137 When C<@opts> is empty, it defaults to restoring the original Perl autovivification behaviour.
138
139 =cut
140
141 sub import {
142  shift;
143  my $hint = 0;
144  if (@_) {
145   $hint = _detag($^H{+(__PACKAGE__)}) || 0;
146   $hint &= ~$bits{$_} for grep exists $bits{$_}, @_;
147  }
148  $^H |= 0x00020000;
149  $^H{+(__PACKAGE__)} = _tag($hint);
150  ();
151 }
152
153 =head1 DEPENDENCIES
154
155 L<perl> 5.8.
156
157 L<XSLoader> (standard since perl 5.006).
158
159 =head1 SEE ALSO
160
161 L<perlref>.
162
163 =head1 AUTHOR
164
165 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
166
167 You can contact me by mail or on C<irc.perl.org> (vincent).
168
169 =head1 BUGS
170
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.
173
174 =head1 SUPPORT
175
176 You can find documentation for this module with the perldoc command.
177
178     perldoc autovivification
179
180 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/autovivification>.
181
182 =head1 ACKNOWLEDGEMENTS
183
184 Matt S. Trout asked for it.
185
186 =head1 COPYRIGHT & LICENSE
187
188 Copyright 2009 Vincent Pit, all rights reserved.
189
190 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
191
192 =cut
193
194 1; # End of autovivification