]> git.vpit.fr Git - perl/modules/autovivification.git/blob - README
This is 0.03
[perl/modules/autovivification.git] / README
1 NAME
2     autovivification - Lexically disable autovivification.
3
4 VERSION
5     Version 0.03
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}}". When the
45         expression would have autovivified, "undef" is returned for a plain
46         fetch, while "keys" and "values" return 0 in scalar context and the
47         empty list in list context.
48
49     *   'exists'
50
51         Turn off autovivification for dereferencing expressions that are
52         parts of an "exists", such as "exists
53         $hashref->{key}[$idx]{$field}". '' is returned when the expression
54         would have autovivified.
55
56     *   'delete'
57
58         Turn off autovivification for dereferencing expressions that are
59         parts of a "delete", such as "delete $hashref->{key}[$idx]{$field}".
60         "undef" is returned when the expression would have autovivified.
61
62     *   'store'
63
64         Turn off autovivification for lvalue dereferencing expressions, such
65         as "$hashref->{key}[$idx]{$field} = $value" or "for
66         ($hashref->{key}[$idx]{$field}) { ... }". An exception is thrown if
67         vivification is needed to store the value, which means that
68         effectively you can only assign to levels that are already defined
69         (in the example, this would require "$hashref->{key}[$idx]" to
70         already be a hash reference).
71
72     *   'warn'
73
74         Emit a warning when an autovivification is avoided.
75
76     *   'strict'
77
78         Throw an exception when an autovivification is avoided.
79
80     Each call to "unimport" adds the specified features to the ones already
81     in use in the current lexical scope.
82
83     When @opts is empty, it defaults to "qw/fetch exists delete/".
84
85   "import @opts"
86     Magically called when "use autovivification" is encountered. Disables
87     the features given in @opts, which can be the same as for "unimport".
88
89     Each call to "import" removes the specified features to the ones already
90     in use in the current lexical scope.
91
92     When @opts is empty, it defaults to restoring the original Perl
93     autovivification behaviour.
94
95 DEPENDENCIES
96     perl 5.8.
97
98     XSLoader (standard since perl 5.006).
99
100 SEE ALSO
101     perlref.
102
103 AUTHOR
104     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
105
106     You can contact me by mail or on "irc.perl.org" (vincent).
107
108 BUGS
109     Please report any bugs or feature requests to "bug-autovivification at
110     rt.cpan.org", or through the web interface at
111     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I
112     will be notified, and then you'll automatically be notified of progress
113     on your bug as I make changes.
114
115 SUPPORT
116     You can find documentation for this module with the perldoc command.
117
118         perldoc autovivification
119
120     Tests code coverage report is available at
121     <http://www.profvince.com/perl/cover/autovivification>.
122
123 ACKNOWLEDGEMENTS
124     Matt S. Trout asked for it.
125
126 COPYRIGHT & LICENSE
127     Copyright 2009 Vincent Pit, all rights reserved.
128
129     This program is free software; you can redistribute it and/or modify it
130     under the same terms as Perl itself.
131