Brad Fitzpatrick > Data-SingletonManager-1.00 > Data::SingletonManager

Download:
Data-SingletonManager-1.00.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.00   Source  

NAME ^

Data::SingletonManager - return/set/clear instances of singletons identified by keys in different namespaces.

SYNOPSIS ^

  package My::Object;
  sub instance {
      my %args;
      ...
      my $key = "$args{userid}-$args{object_id}";
      return Data::SingletonManager->instance(
          namespace => __PACKAGE__,    # default; may omit.
          key => $key,
          creator => sub {
              return __PACKAGE__->new_instance($args{userid},
                                               $args{object_id});
          }
      );
  }

  package main;

  # return all singletons loaded in a namespace:
  @loaded_objs = Data::SingletonManager->instances("My::Object");

  # clear all singletons, in all packages (perhaps on new web request)
  Data::SingletonManager->clear_all;

  # clear all singletons in one namespace
  Data::SingletonManager->clear("My::Object");

DESCRIPTION ^

This is a small utility class to help manage multiple keyed singletons in multiple namespaces. It is not a base class so you can drop it into any of your classes without namespace clashes with methods you might already have, like "new", "instance", "new_instance", etc.

PACKAGE METHODS ^

All methods below are package methods. There are no instance methods.

instance(%args)

Package method to return (or create and save) a keyed instance where %args are:

"namespace"

defaults to the calling package

"key"

scalar key for instance. the (namespace, key) uniquely identifies an instance

"creator"

subref to return the instance if it doesn't already exist

instances([ $namespace ])

Return an array of all loaded instances in a namespace, which defaults to the calling namespace if no namespace is given.

clear([ $namespace ])

Clears all instances in a namespace, which defaults to the calling namespace if no namespace is given.

clear_all

Clears all instances in all namespaces.

AUTHORS ^

Brad Fitzpatrick <brad@danga.com>

COPYRIGHT AND LICENSE ^

Copyright 2005 by Six Apart, Ltd.

License is granted to use and distribute this module under the same terms as Perl itself.

syntax highlighting: