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

Thanks to the strong suggestions of the good folks on this forum about the value of "use strict" I'm making the conversion of my existing code into use strict.

I've been on the -w bandwagon since day one so at least I don't have those potential messes to look forward to.

However, with the switch to use strict I'm not sure how to handle my globals.

I have three libraries I use for different purposes. I've included them into different scripts using "require". One of these libraries establishes about a dozen global vars that I expect to be floating around in all of my scripts.

Eventually I'll start using proper package::var type syntax. But at the moment I'd like to sneak up on that one script at a time.

Hence I'm looking at the use of "use var" or "our" to declare these globals in my libraries.

Is it true that both will work? Or will I lose scope when the program returns from the required library into the main script?

As you might guess I've been doing all my Perl on street corners and now I'm trying to learn it gooder. ;-)

Claude
p.s. If you know of a tutorial for "Strict after the fact" let me know.

  • Comment on "use vars" vs. "our" or Strict after the fact

Replies are listed 'Best First'.
Re: "use vars" vs. "our" or Strict after the fact
by jeroenes (Priest) on Mar 30, 2001 at 16:12 UTC
    You can read up on it in require, use and perlmod. There are also a lot of nodes on this subject.

    I think the core of your question is what scope a required file is evaluated in. Well, that is the same scope as the calling script. So if you call 'script42.pl' in the package 'TrueScope' like:

    package TrueScope; require 'script42.pl';
    Than the perlcode in script42.pl is evaluated in the scope 'TrueScope';

    Jeroen
    "We are not alone"(FZ)
    Update: On second thought, I'd better say some more.

    Praises for you, it's a very good idea to switch to use strict. Your code will be cleaner, and terrible bugs will be caught.

    Consider to use diagnostics as well. It's a powerful tool. Another thing is use constant to declare constants.

    A good thread to catch up on 'our' and 'use strict' is Why is 'our' good?. There is also a FAQ: What's the difference between require and use?.

    Happy coding!

      Many thanks. I've already been chasing down the previous references. I'll look into these two new ones with pleasure.

Re: "use vars" vs. "our" or Strict after the fact
by Desdinova (Friar) on Mar 30, 2001 at 21:38 UTC
    A slight aside here. I try to use global vars sparingly. At my work I notice the most common use of them is to define site specific info (paths to filenames, database names etc.) For those kind of thing that do not change during the running of a script, I perfer to use constant. For me I've found that it makes the purpose of the statement a little clearer as well as not having to be concerend with scope for those things.

    Also I perfer not to use global varibles in several files. When i have to make a change it means I have to open several copies of vi as well as testing the impact on all the scripts. When I use outside files like they are usually subs. I try to define an API for the the subs and and when I update one I can update the wy it handles the passed data so I generally don't hve to change the way the sub is called. One of these days I'm going to try to move them into a 'package' based system.
Re: "use vars" vs. "our" or Strict after the fact
by satchboost (Scribe) on Mar 30, 2001 at 22:04 UTC
    As someone who migrated a medium-sized PERL app to "use strict", I've got a few gimmes and gotchas to share:

    1) You're going to have to change all your for/each loops. Things like "foreach $foo (@bar)" have to become "foreach my $foo (@bar)" and the like. Don't forget that:
    for ($i = 0; $i < $n; $i++) {
    becomes
     for (my $i = 0; $i < $n; $i++) {

    2) Consider using packages like the following:

    package Foo; use Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(Access_Global); my $some_global_variable = 1; sub Access_Global { return $some_global_variable++; } package Bar; use Foo qw(Access_Global); ... my $some_var = Access_Global;

    That way, you have your "global variable", but access it in a very controlled, not-really-global manner. We've got a number of those, such as volatile IDs for triggers, and this works like a charm.

    3) Re-evaluate WHY you want that global. Maybe you're making an assumption as to why something should be global. Maybe it's better to pass it in as a command-line argument. Maybe you only use it in one place. I dunno. We removed some 20% of our global variables by simply realizing we didn't need them.

Re: "use vars" vs. "our" or Strict after the fact
by epoptai (Curate) on Mar 31, 2001 at 04:01 UTC
    One quick and dirty way to do this is to declare some variables in the script, eval require the file, if eval succeeds equate the script var names with the var names in the required file.
    use strict; use vars qw($filevar1 $filevar2 $filevar3); my($scriptvar1,$scriptvar2,$scriptvar3,$required) = 0; my$file = 'somefile'; if(-e $file){ if(eval "require '$file'"){ $scriptvar1 = $filevar1; $scriptvar2 = $filevar2; $scriptvar3 = $filevar3; $required = 1; } }
    Those my variables can be included in the use vars statement instead (but not vice-versa).