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

Is it okay to use vars mulitple times? I sometimes find it would be easier to document my code if I can use vars multiple times, with comments in between:
#!/usr/bin/perl -Tw use strict; # These are for one thing use vars qw/$foo $bar $baz/; # These are for another thing use vars qw/$bimm $bamm $boom/; # Now do one thing or another...
Is this discouraged for some reason or, worse yet, might it break something?

Thanks,
Doran...

Replies are listed 'Best First'.
Re: Multiple 'use vars' lines
by japhy (Canon) on Mar 21, 2001 at 00:33 UTC
      Thanks, I tried it (actually before posting), and it seems to work fine. I just wanted to make sure there was nothing bad happening that I didn't know about. I don't recall ever using a module multiple times in a single script and just wanted to make sure it didn't come back to bite me. -db
Re: Multiple 'use vars' lines
by dws (Chancellor) on Mar 21, 2001 at 01:26 UTC
    An easy (and educational) way to get answers to questions like this is to consult the source.

    vars.pm is quite short, consisting essentially of a single 30-line import routine, of which ~10 lines are interesting. Even if parts of the code use uncommon (though hardly non-standard) trickery, it is quite easy to answer the specific question of whether repeated applications of use vars are cummulative.

    Source is your friend. It will answer all sorts of questions if only you take the time to ask it.

Re: Multiple 'use vars' lines
by larsen (Parson) on Mar 21, 2001 at 03:41 UTC
    It's a good practice to use many use vars lines to group variables with common semantic meaning.

    Now two interesting points from the documentation:

    use vars predeclare global variable names (obsoleted by our())

    and...

    An `our' declares the listed variables to be valid globals within the enclosing block, file, or `eval'. That is, it has the same scoping rules as a "my" declaration, but does not create a local variable. If more than one value is listed, the list must be placed in parentheses. The `our' declaration has no semantic effect unless "use strict vars" is in effect, in which case it lets you use the declared global variable without qual­ ifying it with a package name. (But only within the lexical scope of the `our' declaration. In this it differs from "use vars", which is package scoped.)

      The existence of a new way to do it does not make the old obsolete. And as long as I honestly believe that it is worse than what it replaces, I won't use it.