[sf-perl] Advice on how to expose a class-wide preference
x.sfpug at chatterjee.net
Mon Mar 17 23:28:54 PDT 2008
I'm looking for advice on how to expose a user-settable preference
that affects the behavior of all objects in a given class. I'm working
on a module representing data stringifiable in multiple ways, e.g.:
I'd like to set up string operator overloading, but it's not obvious
what the default stringification style should be; different users will
have different needs. I'd like to allow users to choose how objects
will stringify by default in their code. I can imagine doing this in
two different ways:
1) Have a class-wide preference indicating the default style:
2) Let users set their preference via a magic variable:
local $Class::default_string_style = 'normalized';
The first option seems cleaner, and easier to subclass (were that an
issue); it also allows for better error handling when the user calls
the class method. Unfortunately, setting a class-wide flag also blows
away any prior settings. Two pieces of code using the module will
necessarily overwrite each other's preferences.
Using a variable seems uglier, but allows users to safely intermix
different preference values in different contexts.
Any suggestions on which of these is generally better to use? Are
there other strategies I'm ignoring? Thanks for any advice.
More information about the SanFrancisco-pm