I was wondering what the best practice in Perl is regarding getting – or, more importantly, setting – a global variable of some module by directly accessing $Module::varName in case the module didn’t provide getter/setter method for it.
The reason it smells bad to me is the fact that it sort of circumvents encapsulation. Just because I can do it in Perl, I’m not entirely certain I should (assuming there actually is an alternative such as adding a getter/setter to the module).
It isn’t violating encapsulation if the variable is part of the public API. (If it isn’t that’s another matter.)
I think direct access is preferable as it allows you to take advantage of dynamic scoping:
This makes conflicts with other code using
Moduleless likely.