http://www.perlmonks.org?node_id=149515


in reply to Re: Help needed understanding global variables in Perl
in thread Help needed understanding global variables in Perl

Yes, one of my questions is indeed that.

But what is the standard way of declaring global variables in Perl? With our? With use vars (which now I know that is obsoleted)? With anything else?

The problem is that even if I don't declare and define anything, I can get rid of warnings just by fully qualifying the variables. In other words, the script below seems to be entirely legal:

#!/usr/bin/perl use strict; use warnings; use diagnostics; $main::f = 42; print "The value is $main::f\n";

The problem is that Programming Perl, 2nd ed. (the one that I have) is not very specific about the subject and I've read already quite a bit of the book (chapters 1, 2, 4, 5 and part of 6).

I don't intent on using global variables, but I feel bad for having trouble not understanding such elementary subjects after reading a fair amount of the documentation.

Replies are listed 'Best First'.
Re: Re: Re: Help needed understanding global variables in Perl
by arturo (Vicar) on Mar 06, 2002 at 18:14 UTC

    Yes, this is legal code. The answer to your question is that there *is* no mechanism in Perl for declaring global variables. The point of use strict is to force you to think about what you're doing when it comes to variables. It will not, however, prevent you from shooting yourself in the foot. Perl is just not that kind of language.

    On the more general issue: tied up with the whole "global/local" thang is the notion of packages. 'Ave yourself a look at perldoc -f package while you're exploring these issues.

    Finally, lemme recommend, as I have many times before, Dominus' "Coping with Scoping" (follow the link to his page and thence to his site) and, less enthusiastically, my own proposed scoping tutorial.

    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

    A reply falls below the community's threshold of quality. You may see it by logging in.