Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: $^W or require warnings and import warnings;

by davido (Cardinal)
on Jun 15, 2005 at 17:55 UTC ( [id://466992]=note: print w/replies, xml ) Need Help??


in reply to $^W or require warnings and import warnings;

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

Replies are listed 'Best First'.
Re^2: $^W or require warnings and import warnings;
by salva (Canon) on Jun 15, 2005 at 18:18 UTC
    have you actually tried your code? because I got...
    Use of uninitialized value in concatenation (.) or string at - line 10 +.
    It works because the import warnings sentence inside the BEGIN block changes the value of ${^WARNING_BITS} at compile time.

      I get the following results...

      C:\Perl\bwb>perl -v This is perl, v5.8.6 built for MSWin32-x86-multi-thread (with 3 registered patches, see perl -V for more detail) ... C:\Perl\bwb>perl 466922.pl Warnings on: Use of uninitialized value in concatenation (.) or string at 2.pl line + 10. Warnings off:

      /renz.
      "Call on God, but row away from the rocks."
      --Hunter S. Thompson.

Re^2: $^W or require warnings and import warnings;
by biosysadmin (Deacon) on Jun 15, 2005 at 18:21 UTC
    Good call, this definitely doesn't work the way the author intended. I wondered the same thing initially and tried something eerily similar to your code, but it seemed to warn me on both print statements. When I read your post I was definitely surprised for a few seconds, until I looked at the top of my script and found this:
    #!/usr/bin/perl -w
    So, removing the -w gave me the output you describe.

    I use perl -w in most of my scripts, so it's in my VIM template for Perl scripts. Further proof that it's important to remember your coding habits, lest they sneak up on you when you least expect it. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://466992]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found