Global modular variables were in vogue in the past, but were considered "bad form" as an interface in Modern Perl. It is important to recognize that Perl is now 22-23 years old, and its styles and practices have changed. :) Please note that there are times when it still fits, because there are some very nice features that come with the package variables. As usual, experience and practice show how good a solution can be.
To understand how best to use package variables, you really need to understand how local works. Check out local perldoc help . Local allows you to take a package variable, like (example) $My::Variable in a My package, and create a dynamically limited version of This. Usually, if you change $My::Variable into place, it will affect your entire program and will be saved. For small programs, this may not be a big problem. For large, this can have disastrous side effects. local allows you to temporarily change this variable, limited to your current scope.
Here's how it works:
use 5.012; use warnings; package My; our $Variable = 5; package main; say $My::Variable;
Effectively, it allows you to safely use batch variables. Unfortunately, not everyone knows about local ones, so they often compress a global variable. It is always helpful to document good usage (e.g. local ).
A getter / setter with proper encapsulation of objects prevents this a lot, but not always. To make it work like a local variable does, you will need to do a lot of extra work. The best part is that you can localize the package variable, so that you can make temporary changes very easily, say, for a debug variable. Usually you need to make a template like:
{ my $current_variable My::get_variable(); $My::set_variable($new_value);
With local, it becomes:
{ local $My::Variable = $new_value;
(By the way, I would like you to be able to do this with lexical variables, for the same reason ... but you cannot.) So, for some things, package variables may make sense. It depends on how you want to use it. Such things as
- Debugging variables
- A global configuration that should not / should not change frequently
However, if something needs to be changed on a regular basis, for example
- Regularly used variables (see the awful interface for
File::Find ) - Temporary configuration
- "Object" Variables
Basically, everything that needs to be changed more than once, or in rare situations, or otherwise needs to be encapsulated in the generated object, then I would avoid the package variable.