Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

NEVER do this. Ever. Not even once.

by dragonchild (Archbishop)
on Jul 30, 2001 at 18:24 UTC ( #100879=note: print w/replies, xml ) Need Help??

in reply to Re: Need some help with a dodgy variable
in thread Variable Scope

(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.

Replies are listed 'Best First'.
Re: NEVER do this. Ever. Not even once.
by bikeNomad (Priest) on Jul 30, 2001 at 20:59 UTC
    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.
Re: NEVER do this. Ever. Not even once.
by perchance (Monk) on Jul 30, 2001 at 18:45 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://100879]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-06-24 19:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.