Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Help needed understanding global variables in Perl

by random (Monk)
on Mar 05, 2002 at 18:34 UTC ( #149466=note: print w/ replies, xml ) Need Help??


in reply to Help needed understanding global variables in Perl

I may be misreading your post (it is, as other monks have said, quite a few issues rolled into one) but it seems part of your question is how to catch globals that are declared but not defined. Try using -w or "use warnings" for that.

-Mike-


Comment on Re: Help needed understanding global variables in Perl
Re: Re: Help needed understanding global variables in Perl
by Anonymous Monk on Mar 05, 2002 at 21:46 UTC
    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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://149466]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-07-10 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls