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
    $a and $b are special global variables. See perlvar.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thanks! I would never have thought to check that .. not for such vanilla names as $a and $b. Well, I've learned something. Thanks again.

Re: Unexpected behavior with 'use strict'
by toolic (Bishop) on Jul 04, 2013 at 22:53 UTC
    Also, from strict
    Because of their special use by sort(), the variables $a and $b are exempted from this check.
Re: Unexpected behavior with 'use strict'
by 2teez (Vicar) on Jul 04, 2013 at 22:58 UTC

    So, the moral lesson here is ALWAYS use a good descriptive variable name, it's even self documenting than just a letter variable name.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Unexpected behavior with 'use strict'
by ww (Archbishop) on Jul 04, 2013 at 22:55 UTC
    Extending choroba's bang-on analysis with an Rx:
    Don't use $a or $b as $var names except in the context of sorts

    If you didn't program your executable by toggling in binary, it wasn't really programming!