Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
14 KiB

  1. =head1 NAME
  2. perllexwarn - Perl Lexical Warnings
  3. =head1 DESCRIPTION
  4. The C<use warnings> pragma is a replacement for both the command line
  5. flag B<-w> and the equivalent Perl variable, C<$^W>.
  6. The pragma works just like the existing "strict" pragma.
  7. This means that the scope of the warning pragma is limited to the
  8. enclosing block. It also means that the pragma setting will not
  9. leak across files (via C<use>, C<require> or C<do>). This allows
  10. authors to independently define the degree of warning checks that will
  11. be applied to their module.
  12. By default, optional warnings are disabled, so any legacy code that
  13. doesn't attempt to control the warnings will work unchanged.
  14. All warnings are enabled in a block by either of these:
  15. use warnings ;
  16. use warnings 'all' ;
  17. Similarly all warnings are disabled in a block by either of these:
  18. no warnings ;
  19. no warnings 'all' ;
  20. For example, consider the code below:
  21. use warnings ;
  22. my @a ;
  23. {
  24. no warnings ;
  25. my $b = @a[0] ;
  26. }
  27. my $c = @a[0];
  28. The code in the enclosing block has warnings enabled, but the inner
  29. block has them disabled. In this case that means the assignment to the
  30. scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]">
  31. warning, but the assignment to the scalar C<$b> will not.
  32. =head2 Default Warnings and Optional Warnings
  33. Before the introduction of lexical warnings, Perl had two classes of
  34. warnings: mandatory and optional.
  35. As its name suggests, if your code tripped a mandatory warning, you
  36. would get a warning whether you wanted it or not.
  37. For example, the code below would always produce an C<"isn't numeric">
  38. warning about the "2:".
  39. my $a = "2:" + 3;
  40. With the introduction of lexical warnings, mandatory warnings now become
  41. I<default> warnings. The difference is that although the previously
  42. mandatory warnings are still enabled by default, they can then be
  43. subsequently enabled or disabled with the lexical warning pragma. For
  44. example, in the code below, an C<"isn't numeric"> warning will only
  45. be reported for the C<$a> variable.
  46. my $a = "2:" + 3;
  47. no warnings ;
  48. my $b = "2:" + 3;
  49. Note that neither the B<-w> flag or the C<$^W> can be used to
  50. disable/enable default warnings. They are still mandatory in this case.
  51. =head2 What's wrong with B<-w> and C<$^W>
  52. Although very useful, the big problem with using B<-w> on the command
  53. line to enable warnings is that it is all or nothing. Take the typical
  54. scenario when you are writing a Perl program. Parts of the code you
  55. will write yourself, but it's very likely that you will make use of
  56. pre-written Perl modules. If you use the B<-w> flag in this case, you
  57. end up enabling warnings in pieces of code that you haven't written.
  58. Similarly, using C<$^W> to either disable or enable blocks of code is
  59. fundamentally flawed. For a start, say you want to disable warnings in
  60. a block of code. You might expect this to be enough to do the trick:
  61. {
  62. local ($^W) = 0 ;
  63. my $a =+ 2 ;
  64. my $b ; chop $b ;
  65. }
  66. When this code is run with the B<-w> flag, a warning will be produced
  67. for the C<$a> line -- C<"Reversed += operator">.
  68. The problem is that Perl has both compile-time and run-time warnings. To
  69. disable compile-time warnings you need to rewrite the code like this:
  70. {
  71. BEGIN { $^W = 0 }
  72. my $a =+ 2 ;
  73. my $b ; chop $b ;
  74. }
  75. The other big problem with C<$^W> is the way you can inadvertently
  76. change the warning setting in unexpected places in your code. For example,
  77. when the code below is run (without the B<-w> flag), the second call
  78. to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas
  79. the first will not.
  80. sub doit
  81. {
  82. my $b ; chop $b ;
  83. }
  84. doit() ;
  85. {
  86. local ($^W) = 1 ;
  87. doit()
  88. }
  89. This is a side-effect of C<$^W> being dynamically scoped.
  90. Lexical warnings get around these limitations by allowing finer control
  91. over where warnings can or can't be tripped.
  92. =head2 Controlling Warnings from the Command Line
  93. There are three Command Line flags that can be used to control when
  94. warnings are (or aren't) produced:
  95. =over 5
  96. =item B<-w>
  97. This is the existing flag. If the lexical warnings pragma is B<not>
  98. used in any of you code, or any of the modules that you use, this flag
  99. will enable warnings everywhere. See L<Backward Compatibility> for
  100. details of how this flag interacts with lexical warnings.
  101. =item B<-W>
  102. If the B<-W> flag is used on the command line, it will enable all warnings
  103. throughout the program regardless of whether warnings were disabled
  104. locally using C<no warnings> or C<$^W =0>. This includes all files that get
  105. included via C<use>, C<require> or C<do>.
  106. Think of it as the Perl equivalent of the "lint" command.
  107. =item B<-X>
  108. Does the exact opposite to the B<-W> flag, i.e. it disables all warnings.
  109. =back
  110. =head2 Backward Compatibility
  111. If you are used with working with a version of Perl prior to the
  112. introduction of lexically scoped warnings, or have code that uses both
  113. lexical warnings and C<$^W>, this section will describe how they interact.
  114. How Lexical Warnings interact with B<-w>/C<$^W>:
  115. =over 5
  116. =item 1.
  117. If none of the three command line flags (B<-w>, B<-W> or B<-X>) that
  118. control warnings is used and neither C<$^W> or the C<warnings> pragma
  119. are used, then default warnings will be enabled and optional warnings
  120. disabled.
  121. This means that legacy code that doesn't attempt to control the warnings
  122. will work unchanged.
  123. =item 2.
  124. The B<-w> flag just sets the global C<$^W> variable as in 5.005 -- this
  125. means that any legacy code that currently relies on manipulating C<$^W>
  126. to control warning behavior will still work as is.
  127. =item 3.
  128. Apart from now being a boolean, the C<$^W> variable operates in exactly
  129. the same horrible uncontrolled global way, except that it cannot
  130. disable/enable default warnings.
  131. =item 4.
  132. If a piece of code is under the control of the C<warnings> pragma,
  133. both the C<$^W> variable and the B<-w> flag will be ignored for the
  134. scope of the lexical warning.
  135. =item 5.
  136. The only way to override a lexical warnings setting is with the B<-W>
  137. or B<-X> command line flags.
  138. =back
  139. The combined effect of 3 & 4 is that it will allow code which uses
  140. the C<warnings> pragma to control the warning behavior of $^W-type
  141. code (using a C<local $^W=0>) if it really wants to, but not vice-versa.
  142. =head2 Category Hierarchy
  143. A hierarchy of "categories" have been defined to allow groups of warnings
  144. to be enabled/disabled in isolation.
  145. The current hierarchy is:
  146. all -+
  147. |
  148. +- chmod
  149. |
  150. +- closure
  151. |
  152. +- exiting
  153. |
  154. +- glob
  155. |
  156. +- io -----------+
  157. | |
  158. | +- closed
  159. | |
  160. | +- exec
  161. | |
  162. | +- newline
  163. | |
  164. | +- pipe
  165. | |
  166. | +- unopened
  167. |
  168. +- misc
  169. |
  170. +- numeric
  171. |
  172. +- once
  173. |
  174. +- overflow
  175. |
  176. +- pack
  177. |
  178. +- portable
  179. |
  180. +- recursion
  181. |
  182. +- redefine
  183. |
  184. +- regexp
  185. |
  186. +- severe -------+
  187. | |
  188. | +- debugging
  189. | |
  190. | +- inplace
  191. | |
  192. | +- internal
  193. | |
  194. | +- malloc
  195. |
  196. +- signal
  197. |
  198. +- substr
  199. |
  200. +- syntax -------+
  201. | |
  202. | +- ambiguous
  203. | |
  204. | +- bareword
  205. | |
  206. | +- deprecated
  207. | |
  208. | +- digit
  209. | |
  210. | +- parenthesis
  211. | |
  212. | +- precedence
  213. | |
  214. | +- printf
  215. | |
  216. | +- prototype
  217. | |
  218. | +- qw
  219. | |
  220. | +- reserved
  221. | |
  222. | +- semicolon
  223. |
  224. +- taint
  225. |
  226. +- umask
  227. |
  228. +- uninitialized
  229. |
  230. +- unpack
  231. |
  232. +- untie
  233. |
  234. +- utf8
  235. |
  236. +- void
  237. |
  238. +- y2k
  239. Just like the "strict" pragma any of these categories can be combined
  240. use warnings qw(void redefine) ;
  241. no warnings qw(io syntax untie) ;
  242. Also like the "strict" pragma, if there is more than one instance of the
  243. C<warnings> pragma in a given scope the cumulative effect is additive.
  244. use warnings qw(void) ; # only "void" warnings enabled
  245. ...
  246. use warnings qw(io) ; # only "void" & "io" warnings enabled
  247. ...
  248. no warnings qw(void) ; # only "io" warnings enabled
  249. To determine which category a specific warning has been assigned to see
  250. L<perldiag>.
  251. =head2 Fatal Warnings
  252. The presence of the word "FATAL" in the category list will escalate any
  253. warnings detected from the categories specified in the lexical scope
  254. into fatal errors. In the code below, the use of C<time>, C<length>
  255. and C<join> can all produce a C<"Useless use of xxx in void context">
  256. warning.
  257. use warnings ;
  258. time ;
  259. {
  260. use warnings FATAL => qw(void) ;
  261. length "abc" ;
  262. }
  263. join "", 1,2,3 ;
  264. print "done\n" ;
  265. When run it produces this output
  266. Useless use of time in void context at fatal line 3.
  267. Useless use of length in void context at fatal line 7.
  268. The scope where C<length> is used has escalated the C<void> warnings
  269. category into a fatal error, so the program terminates immediately it
  270. encounters the warning.
  271. =head2 Reporting Warnings from a Module
  272. The C<warnings> pragma provides a number of functions that are useful for
  273. module authors. These are used when you want to report a module-specific
  274. warning to a calling module has enabled warnings via the C<warnings>
  275. pragma.
  276. Consider the module C<MyMod::Abc> below.
  277. package MyMod::Abc;
  278. use warnings::register;
  279. sub open {
  280. my $path = shift ;
  281. if (warnings::enabled() && $path !~ m#^/#) {
  282. warnings::warn("changing relative path to /tmp/");
  283. $path = "/tmp/$path" ;
  284. }
  285. }
  286. 1 ;
  287. The call to C<warnings::register> will create a new warnings category
  288. called "MyMod::abc", i.e. the new category name matches the current
  289. package name. The C<open> function in the module will display a warning
  290. message if it gets given a relative path as a parameter. This warnings
  291. will only be displayed if the code that uses C<MyMod::Abc> has actually
  292. enabled them with the C<warnings> pragma like below.
  293. use MyMod::Abc;
  294. use warnings 'MyMod::Abc';
  295. ...
  296. abc::open("../fred.txt");
  297. It is also possible to test whether the pre-defined warnings categories are
  298. set in the calling module with the C<warnings::enabled> function. Consider
  299. this snippet of code:
  300. package MyMod::Abc;
  301. sub open {
  302. warnings::warnif("deprecated",
  303. "open is deprecated, use new instead") ;
  304. new(@_) ;
  305. }
  306. sub new
  307. ...
  308. 1 ;
  309. The function C<open> has been deprecated, so code has been included to
  310. display a warning message whenever the calling module has (at least) the
  311. "deprecated" warnings category enabled. Something like this, say.
  312. use warnings 'deprecated';
  313. use MyMod::Abc;
  314. ...
  315. MyMod::Abc::open($filename) ;
  316. Either the C<warnings::warn> or C<warnings::warnif> function should be
  317. used to actually display the warnings message. This is because they can
  318. make use of the feature that allows warnings to be escalated into fatal
  319. errors. So in this case
  320. use MyMod::Abc;
  321. use warnings FATAL => 'MyMod::Abc';
  322. ...
  323. MyMod::Abc::open('../fred.txt');
  324. the C<warnings::warnif> function will detect this and die after
  325. displaying the warning message.
  326. The three warnings functions, C<warnings::warn>, C<warnings::warnif>
  327. and C<warnings::enabled> can optionally take an object reference in place
  328. of a category name. In this case the functions will use the class name
  329. of the object as the warnings category.
  330. Consider this example:
  331. package Original ;
  332. no warnings ;
  333. use warnings::register ;
  334. sub new
  335. {
  336. my $class = shift ;
  337. bless [], $class ;
  338. }
  339. sub check
  340. {
  341. my $self = shift ;
  342. my $value = shift ;
  343. if ($value % 2 && warnings::enabled($self))
  344. { warnings::warn($self, "Odd numbers are unsafe") }
  345. }
  346. sub doit
  347. {
  348. my $self = shift ;
  349. my $value = shift ;
  350. $self->check($value) ;
  351. # ...
  352. }
  353. 1 ;
  354. package Derived ;
  355. use warnings::register ;
  356. use Original ;
  357. our @ISA = qw( Original ) ;
  358. sub new
  359. {
  360. my $class = shift ;
  361. bless [], $class ;
  362. }
  363. 1 ;
  364. The code below makes use of both modules, but it only enables warnings from
  365. C<Derived>.
  366. use Original ;
  367. use Derived ;
  368. use warnings 'Derived';
  369. my $a = new Original ;
  370. $a->doit(1) ;
  371. my $b = new Derived ;
  372. $a->doit(1) ;
  373. When this code is run only the C<Derived> object, C<$b>, will generate
  374. a warning.
  375. Odd numbers are unsafe at main.pl line 7
  376. Notice also that the warning is reported at the line where the object is first
  377. used.
  378. =head1 TODO
  379. perl5db.pl
  380. The debugger saves and restores C<$^W> at runtime. I haven't checked
  381. whether the debugger will still work with the lexical warnings
  382. patch applied.
  383. diagnostics.pm
  384. I *think* I've got diagnostics to work with the lexical warnings
  385. patch, but there were design decisions made in diagnostics to work
  386. around the limitations of C<$^W>. Now that those limitations are gone,
  387. the module should be revisited.
  388. document calling the warnings::* functions from XS
  389. =head1 SEE ALSO
  390. L<warnings>, L<perldiag>.
  391. =head1 AUTHOR
  392. Paul Marquess