[sf-perl] Advice on how to expose a class-wide preference

Steve Fink sphink at gmail.com
Tue Mar 18 13:12:46 PDT 2008

On Mon, Mar 17, 2008 at 11:28 PM, Anirvan Chatterjee
<x.sfpug at chatterjee.net> wrote:
>  1) Have a class-wide preference indicating the default style:
>         Class->default_string_style('normalized');
>  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.

Just to add to the set of possibilities, you could make the settings
only apply to the package that requested them. So the syntax could
either be your original #1:


or you could make it explicit

  Class->default_string_style(__PACKAGE__, 'normalized')

default_string_style() would then use caller() or the passed-in
package name to record who wants what, and the stringification would
use caller() to decide whose setting to use.

I'm not recommending it, just pointing it out.

If you really wanted to be able to use local() but still have
package-scoping, you could always do


and have stringification use caller() to look up that variable in its
caller's package.


More information about the SanFrancisco-pm mailing list