in reply to Detecting scoping/namespace conflicts

gregw asked:

What I now want to know is, is there some useful trick or mechanism like 'use strict' or 'perl -w' that I could use to catch this class of bug more easily?

There are a couple of things you can do. First, local only works on package variables and the pseudo-global "special" variables such as $_ and friends. Since global variables make "action at a distance" a considerable problem, consider eliminating them from your code and stick to lexical variables declared with my (which are still package variables, they just default to the %main:: namespace).

If, for some reason, this is not feasible, simply don't reuse the variable name. Unless you're doing some really funky stuff, there's no need for local unless you're dealing with Perl's built-in globals.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

  • Comment on (Ovid) Re: Detecting scoping/namespace conflicts

Replies are listed 'Best First'.
Re: Re: Detecting scoping/namespace conflicts
by Kanji (Parson) on Apr 03, 2002 at 22:20 UTC
    [...] consider eliminating them from your code and stick to lexical variables declared with my [...]

    But using my instead doesn't catch re-using of variables unless they're in the same scope, which everything inside gregw's if (blah blah) { ... } is not.

        --k.


Re: (Ovid) Re: Detecting scoping/namespace conflicts
by gregw (Beadle) on Apr 04, 2002 at 12:35 UTC
    I didn't mean to re-use the variable name. What happened is that originally I was just using it in the inner scope, and then when I started realizing I needed it in the outer scope, I forgot to remove the initialization from the inner scope.

    And one clarification; probably a mistaken simplification on my part in my example. I was using $pkgname::usefuldata in my code, not just a plain $usefuldata... something I resorted to when trying to move a cron script I created into a web application.

    So really, to avoid this in the future, I have to write and keep rewriting my code to use 'my' all the time, rather than expecting some compile time or run-time checking to warn me. Is that basically right?