The code is actually sort of a failed attempt.

The author seems to realize that the use directive occurs at compiletime, and is not easily conditionalized. So he uses the runtime-executed 'require' method of invoking a module conditionally. However, he does it within a lexical block (  if(....){ lexical block } ). The warnings pragma is lexically scoped to the block in which it is invoked, so he is only turning warnings on for a brief instant, which will not have any effect outside the if conditional block. Even if it did extend past the if(){...} block, it would be limited by the lexical scope of the BEGIN{...} block.

You can observe this fact by modifying the code as follows:

BEGIN{ if( $] >= 5.006_000 ) { require warnings; import warnings; print "Warnings on: $hi\n"; } else { $^W = 1; } } print "Warnings off: $hi\n";

With this code, the output will be something to the effect of:

Warnings on: Use of uninitialized value in mytest.pl, line xxx.... Warnings off:

In other words, the second use of uninitialized value is in a lexical scope not covered by warnings, and thus the use of uninitialized value is not reported the second time.


Dave


In reply to Re: $^W or require warnings and import warnings; by davido
in thread $^W or require warnings and import warnings; by szabgab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.