phramus has asked for the wisdom of the Perl Monks concerning the following question:
Hi, guys. I am seeing some Perl weirdness. The following code compiles and runs without complaint, even though 'use strict' is on. I've tried both 5.16 and 5.14 releases. (Also, I realize the code does nothing useful. It's just some learning I'm doing.)
use v5.14; use warnings; use autodie; $a = "main"; print "$a\n"; package Cat; $a = "a cat"; package Dog; $a = "a dog"; print "$a\n"; package Cat; print "$a\n"; print "$::a\n"; package main; $b = 17; print "$b\n";
When I first entered the code and compiled it, I got a bunch of errors -- but not the "requires explicit package name" errors that I was expecting. Rather, Perl complained that the $a variables in Cat and Dog namespaces were shadowing the $a in main. Weird. So I turned off 'use strict', and everything worked just fine. But when I turned 'use strict' back on, everything STILL worked just fine. But wait .. that's not all! With 'use strict' still on, I can make certain changes, like adding blank lines and adding '$b = "a boy";' to the Cat and Dog namespaces. These changes cause no problems. But as soon as I add a variable with a name that hasn't been seen before ... like '$c = "another cat";' ... I get the "requires explicit package name" error. Finally, I was able to make a new file that duplicated this behavior without me ever having to turn off strict to begin with. I'm hoping someone can shed some light on this. For the record: I'm running Win 7 64-bit and using Eclipse with the EPIC plug-in. I have also edited the scripts outside Eclipse and run them from the command line. My 5.16 Perl was the 64-bit version, while my 5.14 Perl is the 32-bit version. That's about all the info I can think to provide.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unexpected behavior with 'use strict'
by choroba (Cardinal) on Jul 04, 2013 at 22:45 UTC | |
by phramus (Novice) on Jul 04, 2013 at 22:54 UTC | |
|
Re: Unexpected behavior with 'use strict'
by toolic (Bishop) on Jul 04, 2013 at 22:53 UTC | |
|
Re: Unexpected behavior with 'use strict'
by 2teez (Vicar) on Jul 04, 2013 at 22:58 UTC | |
|
Re: Unexpected behavior with 'use strict'
by ww (Archbishop) on Jul 04, 2013 at 22:55 UTC |