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


in reply to Variable Scope

If I understand correctly, you can make the variable global by adding :: after the $,@, or whatever. If you do so you will not be able to scope it using my, so you can initialze it at your leisure. For example:
#!/usr/bin/perl -w use strict; $::var = 0; print "Variable is [$::var]\n"; changeVar(); print "Variable is now [$::var]\n"; sub changeVar { $::var = 2; }

Hope this helps,
--- perchance

Replies are listed 'Best First'.
NEVER do this. Ever. Not even once.
by dragonchild (Archbishop) on Jul 30, 2001 at 18:24 UTC
    (Please do not take the following node as a personal insult. It was not meant as such.)

    I must interject that this, while perfectly legal, is horrible programming practice. I've been working on ripping out globals written in exactly this fashion for the past 3 weeks on a side project I wasn't even hired to do.

    Do NOT use ::

    • for variable names unless there simply is not other way
    • to import variables from another package
    • to move variables around within a package
    • to initialize variables for use in another package
    Most especially, NEVER USE THEM TO GET AROUND STRICT. Ever. Never Ever. Not even once, as a joke.

    In fact, please forget that :: exists for variable names. It'll make your life so much easier in the long run, and the people who come after you who have to maintain your code will shower many blessings (instead of curses) upon your head.

      Interesting suggestion. What's wrong with this usage (which I find myself using frequently)? :
      use strict; package MyClass; @MyClass::ISA = 'Whatever';
        Nothing's intrinsically wrong with it, so far as that goes. However, what ends up happening is that you have a global variable in one place. This could lead to a slippery slope. I'm not saying that someone of your experience will do so, but it could very well happen.

        Instead of that syntax, I would recommend one of the following:

        package MyClass; @ISA = qw(Whatever); use strict; ----- use strict; use vars '@ISA'; package MyClass; @ISA = qw(Whatever); ----- use strict; use 5.6.0; package MyClass; our @ISA = qw(Whatever);
        The first two still use globals, and are for pre-5.6.0 or before. If you require that 5.6.0 or higher be used, there's absolutely not reason not to use our. None.
      Care to explain why?
      For quite a long time (before we started Object-Orienting the project) we used global (::) variables to hold our internal database. Though I can understand why under certain circumstances it might not be wise, to that end we found it appropriate.(And there were mounds of vars there).
      In fact, I have often found myself looking around in other people's code, trying to figure out which of the hundreds of hashes, arrays and scalar defined at the beginning of the script (outside the scope) they were referring to, and where they were changed. At least when they were ::'ed I knew I had to go chase them...
      I appreciate the problems this can cause across packages, but sometimes your scripts standalone.

      If I am missing something - please tell me,
      --- perchance
        I understood dragonchild to mean that using $::somevar is an awful practice. I quite agree. What's the point of telling Perl, "I want you to alert me to potentially hazardous programming" when you go out of your way to violate it?

        As for global variables, they're hard to track down, they can have unfortunate side effects, they can make your code difficult to follow, they break encapsulation, they reduce reusability, and they can lead to severe contortions when you add new things.

        That's not to say that they're forbidden. bikeNomad points out a place where it's absolutely necessary. It's even useful.

        It's just that, in general, the places where you really need to use global variables are few and far between. Appropriately scoped lexicals exist for a reason.