1indirect(3) User Contributed Perl Documentation indirect(3)
2
3
4
6 indirect - Lexically warn about using the indirect method call syntax.
7
9 Version 0.39
10
12 In a script :
13
14 no indirect; # lexically enables the pragma
15 my $x = new Apple 1, 2, 3; # warns
16 {
17 use indirect; # lexically disables the pragma
18 my $y = new Pear; # legit, does not warn
19 {
20 # lexically specify an hook called for each indirect construct
21 no indirect hook => sub {
22 die "You really wanted $_[0]\->$_[1] at $_[2]:$_[3]"
23 };
24 my $z = new Pineapple 'fresh'; # croaks 'You really wanted...'
25 }
26 }
27 try { ... }; # warns if try() hasn't been declared in this package
28
29 no indirect 'fatal'; # or ':fatal', 'FATAL', ':Fatal' ...
30 if (defied $foo) { ... } # croaks, note the typo
31
32 Global uses :
33
34 # Globally enable the pragma from the command-line
35 perl -M-indirect=global -e 'my $x = new Banana;' # warns
36
37 # Globally enforce the pragma each time perl is executed
38 export PERL5OPT="-M-indirect=global,fatal"
39 perl -e 'my $y = new Coconut;' # croaks
40
42 When enabled, this pragma warns about indirect method calls that are
43 present in your code.
44
45 The indirect syntax is now considered harmful, since its parsing has
46 many quirks and its use is error prone : when the subroutine "foo" has
47 not been declared in the current package, "foo $x" actually compiles to
48 "$x->foo", and "foo { key => 1 }" to "'key'->foo(1)". Please refer to
49 the "REFERENCES" section for a more complete list of reasons for
50 avoiding this construct.
51
52 This pragma currently does not warn for core functions ("print", "say",
53 "exec" or "system"). This may change in the future, or may be added as
54 optional features that would be enabled by passing options to
55 "unimport".
56
57 This module is not a source filter.
58
60 "unimport"
61 no indirect;
62 no indirect 'fatal';
63 no indirect hook => sub { my ($obj, $name, $file, $line) = @_; ... };
64 no indirect 'global';
65 no indirect 'global, 'fatal';
66 no indirect 'global', hook => sub { ... };
67
68 Magically called when "no indirect @opts" is encountered. Turns the
69 module on. The policy to apply depends on what is first found in @opts
70 :
71
72 · If it is a string that matches "/^:?fatal$/i", the compilation will
73 croak when the first indirect method call is found.
74
75 This option is mutually exclusive with the 'hook' option.
76
77 · If the key/value pair "hook => $hook" comes first, $hook will be
78 called for each error with a string representation of the object as
79 $_[0], the method name as $_[1], the current file as $_[2] and the
80 line number as $_[3]. If and only if the object is actually a
81 block, $_[0] is assured to start by '{'.
82
83 This option is mutually exclusive with the 'fatal' option.
84
85 · If none of "fatal" and "hook" are specified, a warning will be
86 emitted for each indirect method call.
87
88 · If @opts contains a string that matches "/^:?global$/i", the pragma
89 will be globally enabled for all code compiled after the current
90 "no indirect" statement, except for code that is in the lexical
91 scope of "use indirect". This option may come indifferently before
92 or after the "fatal" or "hook" options, in the case they are also
93 passed to "unimport".
94
95 The global policy applied is the one resulting of the "fatal" or
96 "hook" options, thus defaults to a warning when none of those are
97 specified :
98
99 no indirect 'global'; # warn for any indirect call
100 no indirect qw<global fatal>; # die on any indirect call
101 no indirect 'global', hook => \&hook # custom global action
102
103 Note that if another policy is installed by a "no indirect"
104 statement further in the code, it will overrule the global policy :
105
106 no indirect 'global'; # warn globally
107 {
108 no indirect 'fatal'; # throw exceptions for this lexical scope
109 ...
110 require Some::Module; # the global policy will apply for the
111 # compilation phase of this module
112 }
113
114 "import"
115 use indirect;
116
117 Magically called at each "use indirect". Turns the module off.
118
119 As explained in "unimport"'s description, an "use indirect" statement
120 will lexically override a global policy previously installed by "no
121 indirect 'global', ..." (if there's one).
122
124 "msg"
125 my $msg = msg($object, $method, $file, $line);
126
127 Returns the default error message that "indirect" generates when an
128 indirect method call is reported.
129
131 "I_THREADSAFE"
132 True iff the module could have been built with thread-safety features
133 enabled.
134
135 "I_FORKSAFE"
136 True iff this module could have been built with fork-safety features
137 enabled. This will always be true except on Windows where it's false
138 for perl 5.10.0 and below .
139
141 "Indirect call of method "%s" on object "%s" at %s line %d."
142 The default warning/exception message thrown when an indirect method
143 call on an object is found.
144
145 "Indirect call of method "%s" on a block at %s line %d."
146 The default warning/exception message thrown when an indirect method
147 call on a block is found.
148
150 "PERL_INDIRECT_PM_DISABLE"
151 If this environment variable is set to true when the pragma is used for
152 the first time, the XS code won't be loaded and, although the
153 'indirect' lexical hint will be set to true in the scope of use, the
154 pragma itself won't do anything. In this case, the pragma will always
155 be considered to be thread-safe, and as such "I_THREADSAFE" will be
156 true. This is useful for disabling "indirect" in production
157 environments.
158
159 Note that clearing this variable after "indirect" was loaded has no
160 effect. If you want to re-enable the pragma later, you also need to
161 reload it by deleting the 'indirect.pm' entry from %INC.
162
164 The implementation was tweaked to work around several limitations of
165 vanilla "perl" pragmas : it's thread safe, and does not suffer from a
166 "perl 5.8.x-5.10.0" bug that causes all pragmas to propagate into
167 "require"d scopes.
168
169 Before "perl" 5.12, "meth $obj" (no semicolon) at the end of a file is
170 not seen as an indirect method call, although it is as soon as there is
171 another token before the end (as in "meth $obj;" or "meth $obj 1"). If
172 you use "perl" 5.12 or greater, those constructs are correctly
173 reported.
174
175 With 5.8 perls, the pragma does not propagate into "eval STRING". This
176 is due to a shortcoming in the way perl handles the hints hash, which
177 is addressed in perl 5.10.
178
179 The search for indirect method calls happens before constant folding.
180 Hence "my $x = new Class if 0" will be caught.
181
183 Numerous articles have been written about the quirks of the indirect
184 object construct :
185
186 · <http://markmail.org/message/o7d5sxnydya7bwvv> : Far More Than
187 Everything You've Ever Wanted to Know about the Indirect Object
188 syntax, Tom Christiansen, 1998-01-28.
189
190 This historical post to the "perl5-porters" mailing list raised
191 awareness about the perils of this syntax.
192
193 · <http://www.shadowcat.co.uk/blog/matt-s-trout/indirect-but-still-fatal>
194 : Indirect but still fatal, Matt S. Trout, 2009-07-29.
195
196 In this blog post, the author gives an example of an undesirable
197 indirect method call on a block that causes a particularly
198 bewildering error.
199
201 perl 5.8.1.
202
203 A C compiler. This module may happen to build with a C++ compiler as
204 well, but don't rely on it, as no guarantee is made in this regard.
205
206 Carp (standard since perl 5), XSLoader (since perl 5.6.0).
207
209 Vincent Pit "<vpit@cpan.org>".
210
211 You can contact me by mail or on "irc.perl.org" (vincent).
212
214 Please report any bugs or feature requests to "bug-indirect at
215 rt.cpan.org", or through the web interface at
216 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=indirect>. I will be
217 notified, and then you'll automatically be notified of progress on your
218 bug as I make changes.
219
221 You can find documentation for this module with the perldoc command.
222
223 perldoc indirect
224
226 Bram, for motivation and advices.
227
228 Andrew Main and Florian Ragwitz, for testing on real-life code and
229 reporting issues.
230
232 Copyright 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2019
233 Vincent Pit, all rights reserved.
234
235 This program is free software; you can redistribute it and/or modify it
236 under the same terms as Perl itself.
237
238
239
240perl v5.30.1 2020-01-30 indirect(3)