Object::GlobalContainer - A global singleton object container


 use Object::GlobalContainer;
 my $OC = Object::GlobalContainer->new();
 print $OC->get('abc');
 print $OC->get('first/second/third');

 ## per default it works as singleton!
 use Object::GlobalContainer;

 my $OC1 = Object::GlobalContainer->new();
 my $OC2 = Object::GlobalContainer->new();
 print $OC2->get('abc'); ## returns also 123 !

 # to disable singleton behaviour, write:
 my $OC = Object::GlobalContainer->new( notglobal => 1 );

 # loading a class
 my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456);


The GlobalContainer is another object container and very similar to Object::Container and Object::Registrar, but it supports deep hash storages. It means you can save your data by using a path notation like 'foo/bar/more'. And that is not handled as a string, but a hash.






 my $classobject = class($path, $classname, %classparam);

Creates an instance of the given class and sets it to the given path. it also returns the new object. It is managed as a singleton, what means, if there is already an object in the given path, it won't instantiate the class again. It will call the constructor of the class with given parameters. If it is already loaded onto given path, it won't load it again. If you want to force a new load, please delete it first or replace it with set().

 my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456);

I think the way Object::Container register classes, by it's own name, is against the idea of an object container. Because the getting part should not need to know what exactly has been set, to keep the application flexible. That is why I am setting it into the given path and not the classname.

If for example dealing with mod_perl and keeping the application in memory, it is very nice to re-run that code, but automatically skipping a new instantiation.

What means that that line:

 my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456);

will be executed in a different way when running it the second time, if it still is in memory. Then it does not instantiate the class again, but just returns the existing instance.

Otherwise you would need to build your own complex if/else line.



deletes an entry by removing it from the hash


 my $boolean = exists($path);

Validates if an entry (key) exists.


 my $scalar | reference = get($path);

returns a value by given key, which can be a path.


 set($key1, $value1, $key2, $value2);

Adds an object (any value or reference). Expects a key and a value. The key may be a path with slash delimiter. It will be stored as nested hash nodes.

hash storage

To be honest, I was not lucky with the plain store other classes provide. What means, to have only one string as identifier. Often you have already hash structures and you want to store them, but maybe read them with one simple command and not two.

The classical way, other object container usually work:

 my $data = {
      foo => {
               bar => {
                        more => '123',


 print store->get('some/where')->{'foo'}->{'bar'}->{'more'};

Here you had to use the get() plus the hash syntax to access the content. With Object::GlobalContainer it is easier to access the content directly:

 print store->get('some/where/foo/bar/more');

Because the used string (path) is realy a path for a hash structure.


As shown in synopsis's example, the default behaviour is to work as singleton, because you usually want to share things globaly. Disable that by using the flag 'notglobal' in the constructor.

singleton, off (notglobal)

If you want to disable the singleton behaviour, you can use 'notglobal' to do so:

 my $OC1 = Object::GlobalContainer->new( notglobal => 1 );
 my $OC2 = Object::GlobalContainer->new( notglobal => 1 );

 print $OC1->get('abc'); ## returns 123 !
 print $OC2->get('abc'); ## returns 456 !

Both instances act as them self, as own separate object.

imported function

The most elegant way to deal with that global container, is to use a local funtion:

 use Object::GlobalContainer 'objcon';



Here it installs the function 'objcon' locally to access the container. Combined with the default behaviour as singleton, it is quite usefull. Please note, the name of the method you can choose by yourself and may be different in every class.

Do not forget to call the constructor first!


The default delimiter for a path is a slash like in 'a/b/c'. But you can change it, by setting 'delimiter' with the constructor:

 my $OC = Object::GlobalContainer->new( delimiter => '.' );

If using the imported function access method, you can change the delimter via a method call:

 use Object::GlobalContainer 'objcon';



Andreas Hernitscheck ahernit(AT)


You can redistribute it and/or modify it under the conditions of LGPL.