Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: declare globally

by CharlesClarkson (Curate)
on Jan 09, 2002 at 13:06 UTC ( #137412=note: print w/replies, xml ) Need Help??

in reply to declare globally

Many times I find that what people call global is just a file scoped variable. In this case a lexical or constant works fine. I think the biggest mistake made with global variables is not finding out what global means to the questioner.

How do you define "global"? Why do you want to use a global variable? Is this variable to be read-only? Are you passing the variable amongst modules or scripts or just in this one script?

Charles K. Clarkson

Replies are listed 'Best First'.
Re: Re: declare globally
by Parham (Friar) on Jan 09, 2002 at 16:29 UTC
    all of your answers were very well put :). The reason for the question (and it is valid in my point of view) is that i have variables being passed through several scripts and subroutines and instead of continuously passing them around and around, i just wanna keep them global. I'm not worried about losing track of my global variables, because i know all of my global variables start with $global_(variablenameinhere) and i do keep all my variables named accordingly with prefixes of what they do or what they're there for.

    Even with perl's implementation of 'our' as an internal function, will it still be wiser to use 'use' instead of 'our'?

      You really should read the article at this node: 'our' is not 'my', if you haven't already. It specifically addresses your question about whether (or when) to use use or our.

      What it perhaps fails to address is the fundamental difference between lexical variables (declared with 'my') and package globals.

      Originally, before there were such a thing as lexical variables, ALL variables were [package] globals. Global variables involve underlying constructs called typeglobs, which are basically symbol table elements. One typeglob is created for each unique symbol name in a package, and is shared among various objects with the same name (but different types).

      Therefore a package global, $foo shares its typeglob with the package global, @foo, and with the package global, %foo, and with the file handle, foo, ... you get the idea.

      There is an entire body of literature describing this, probably far better than I ever could, so I won't go into further detail here (try using Super Search), except to say that a given typeglob contains what may be thought of as "slots" for each kind of thing (variable or handle) that Perl can support. There is a scalar slot, array slot, hash slot, file handle slot, code slot, etc.

      For at least scalars, arrays and hashes, the value that goes into the slot is a reference to the appropriate type (or nothing, i.e., undef). So when you take a reference of a package global, using the notation, say, \@arr, what you are doing is simply extracting whatever is in the array slot of the typeglob named 'arr'. The entire typeglob may, itself, be referred to as *arr, should you ever want to do this. Legacy code (i.e., perl4) may contain some typeglob manipulation, since that was pretty much the only way to do several things, such as passing around filehandles.

      Which (finally) brings me to lexicals. These have two fundamental differences from package globals. First, they are actually created on the stack (and therefore are destroyed when they go out of scope), and second, they do NOT use typeglobs (which has more subtle implications).

      As such, same-named lexical variables of different types do not share symbol table space. They also cannot be manipulated with the typeglob notation (*variable), but they have the distinct advantage of being destroyed upon going out of scope. Some of the power of Perl (such as aliasing variables and whatnot) cannot be used with lexicals. For example, if $main::foo contained the value, 'hello' and if you set \*main::bar = \*main::foo, then $main::bar would thereafter be an alias, indeed another name, for the scalar value referred to by $main::foo (similar to the concept of "hard links" on UNIX filesystems). Changing the value of $main::foo changes the value of $main::foo and vice versa.

      So, if you are struggling with the answer to the question, "Should I use a file-scoped lexical or a global?" perhaps understanding the underlying difference between the two may help, somewhat.


      You can give a man a fish and feed him for a day ...
      Or, you can
      teach him to fish and feed him for a lifetime

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2018-09-20 07:34 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (173 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!