|Just another Perl shrine|
Help needed understanding global variables in Perlby Anonymous Monk
|on Mar 05, 2002 at 08:22 UTC||Need Help??|
Anonymous Monk has asked for the
wisdom of the Perl Monks concerning the following question:
Hi. I'm a newbie Perl programmer (but a relatively experienced programmer in C) and I'm having trouble understanding the use of global variables in Perl. I would appreciate if people could help me.
The problem that I'm having is actually very simple: how exactly do I define (rather than declare) a global variable in Perl? (I know that declaring the variable may be done with use vars.)
I see that even under use strict, I don't have to declare/define global variables in Perl: I just need to fully qualify them and they're already there. Is this correct? If it is, then is there any kind of "super" use strict, which helps me catch typos that my programs may have?
Also, is there any way of defining a variable like C's static (i.e., a global variable with local scope). I see that Perl's our is almost what I want, but the variable declared with our in a function is still accessible outside the function if I use the variable's fully qualified name. I fear that I don't completely understand our.
In fact, I have trouble understanding the difference between our ($i); and use vars qw($i); Can anybody help with the difference? They don't seem different to the eyes of this poor Perl newbie.
Thanks in advance for any comments.