Package Variable Defaults

package Your::Point;

use Badger::Class
    version     => 2.718,
    debug       => 0,
    base        => 'Badger::Base',
    config      => 'x|class:X y|class:Y',
    set_methods => 'x y',
    init_method => 'configure',
    throws      => 'point';

our $X = 0;
our $Y = 0;
Thus Spake Andy:

Here we've tweaked the config option and changed our get_methods accessors to set_methods mutators.

The config option gives you some flexibility in where it will go looking for configuration values. You can tell it to look for default values in package variables (pkg:VARNAME and class:VARNAME), environment variables (env:VARNAME), or by calling other methods (method:METHODNAME). The latter also includes constant subroutines which you can call as class/object methods.

The generated configure() method doesn't do any kind of validation - there are plenty of fine modules already out there that will do that for you (which we might plugin at some point). Rather, its primary purpose is to find the most appropriate value for a variable which could be defined in one of several places. In the above example, we tell it to look for the x parameter in the $config hash as before, but if that is undefined then to use the value defined in the X class variable. Which is a roundabout way of saying the $X package variable.

However... and here's another science bit... there is a subtle but important difference between package variables and class variables. If we had written pkg:X instead of class:X then we would have got the value in the $X package variable (which we declare and define to be 0 down at the bottom there). This would be true even if we create a subclass of Your::Point - it will always look at the $Your::Point::X package variable for a default value if one isn't explicitly defined in the object's configuration parameters. However, when we define a class variable using class:X then it will look for the $X variable in the package of the subclass instead of the Your::Point base class. If the package variable isn't defined in the subclass then it will fall back on the value defined in the base class (following the entire inheritance tree if necessary).