chrism01 has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I always try to enforce safe coding, so I always use
#!/usr/bin/perl -w use strict;
at the top of my progs.
however, I've come across an interesting anomaly, viz:
#!/usr/bin/perl -w use strict; { package cfg; use strict; } $cfg::test_str = "cfg test"; print "$cfg::test_str\n";
This code 'works' as in it prints out the string val & I do not get any warnings / errors about undeclared var $cfg::test_str, even though I think I should.
Is there any way to make the prog enforce declaration of the pkg var?
Thx
Chris

Replies are listed 'Best First'.
Re: Package declaration & use strict;
by Aristotle (Chancellor) on Jan 13, 2006 at 02:00 UTC

    No. strict never complains about fully qualified package variable accesses. This is considered a feature.

    If the reason you are wondering about this is because you think you need to write code with a lot of fully qualified package variable accesses and you want to be protected, you should probably rethink your code structure.

    Makeshifts last the longest.

      No, not too worried about my code as such. It's just that after about 20 yrs of Prog/DBA/Sysadmin etc, (inc a long stint in C) I'm a bit paranoid. I like to nail down my code real tight and not allow things like undeclared vars etc.
      Pity there's no 'use strict strict;' as it were ;-)
      Cheers
      Chris
Re: Package declaration & use strict;
by davido (Cardinal) on Jan 13, 2006 at 05:42 UTC

    Maybe "noFullyQualified" could be an option for the recently discussed (but at this point only conceptual) pragma, "use stricter;"

    But then that would conflict with the proposed "names" option, which forbids names in mixed caps. ;)


    Dave

      Or a new Perl-Critic policy ;)

      Ordinary morality is for ordinary people. -- Aleister Crowley
Re: Package declaration & use strict;
by dragonchild (Archbishop) on Jan 13, 2006 at 13:55 UTC
    At some point, you have to come to terms with the realization that there is simply no way to enforce any policy upon Perl code. There is a way around every single safeguard you might try to put up. (Yes, even inside-out objects.)

    The solution I've found that works best is code reviews. If you see something that violates the coding standards your organization has in place, then you need to flag it in the code review.

    Aren't doing code reviews? Why not do the next best thing and pair-program. If Joe writes the test, Bill writes the code. If Bill writes the test, Joe writes the code. End result is both tests and reviews. This works really well in distributed environments, too. :-)


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Package declaration & use strict;
by TomDLux (Vicar) on Jan 13, 2006 at 18:43 UTC

    In Perl you are allowed to re-open a module and add more code to it. So add some routines to set package variables, if that's needed. Package variables are nasty, but if you can't avoid them, at least you can restrict references to the variable to a single place, and have strict verifying you set the right ones.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Package declaration & use strict;
by webengr (Pilgrim) on Jan 14, 2006 at 00:23 UTC
    Perhaps the "use stricter" pragma option could be "noautovivification". It seems that autovivification is what the OP desires to disable.
    PCS
      Yeah, "noautovivification" sounds like what I had in mind ie like the 'C' lang.
      I (try to) always declare my vars before use, but the other day I accidentally didn't for 1 var (nobody's perfect), which is how I discovered 'use strict;' isn't as strict as I thought.
      I was hoping to be able to protect myself from me ;-) and any other programmer as well.