| 1 | package Object::GlobalContainer; # A global singleton object container | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | # The GlobalContainer is another object container and very similar to Object::Container and Object::Registrar, but | ||
| 6 | # it supports deep hash storages. It means you can save your data by using a path notation like 'foo/bar/more'. | ||
| 7 | # And that is not handled as a string, but a hash. | ||
| 8 | # | ||
| 9 | # SYNOPSIS | ||
| 10 | # ======== | ||
| 11 | # | ||
| 12 | # use Object::GlobalContainer; | ||
| 13 | # | ||
| 14 | # my $OC = Object::GlobalContainer->new(); | ||
| 15 | # | ||
| 16 | # $OC->set('abc','123'); | ||
| 17 | # $OC->set('first/second/third','456'); | ||
| 18 | # | ||
| 19 | # print $OC->get('abc'); | ||
| 20 | # print $OC->get('first/second/third'); | ||
| 21 | # | ||
| 22 | # | ||
| 23 | # ## per default it works as singleton! | ||
| 24 | # use Object::GlobalContainer; | ||
| 25 | # | ||
| 26 | # my $OC1 = Object::GlobalContainer->new(); | ||
| 27 | # my $OC2 = Object::GlobalContainer->new(); | ||
| 28 | # | ||
| 29 | # $OC1->set('abc','123'); | ||
| 30 | # print $OC2->get('abc'); ## returns also 123 ! | ||
| 31 | # | ||
| 32 | # # to disable singleton behaviour, write: | ||
| 33 | # my $OC = Object::GlobalContainer->new( notglobal => 1 ); | ||
| 34 | # | ||
| 35 | # # loading a class | ||
| 36 | # my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456); | ||
| 37 | # | ||
| 38 | # | ||
| 39 | # singleton | ||
| 40 | # ========= | ||
| 41 | # As shown in synopsis's example, the default behaviour is to work as singleton, because you usually want to | ||
| 42 | # share things globaly. Disable that by using the flag 'notglobal' in the constructor. | ||
| 43 | # | ||
| 44 | # | ||
| 45 | # singleton, off (notglobal) | ||
| 46 | # ========================== | ||
| 47 | # If you want to disable the singleton behaviour, you can use 'notglobal' to do so: | ||
| 48 | # | ||
| 49 | # my $OC1 = Object::GlobalContainer->new( notglobal => 1 ); | ||
| 50 | # my $OC2 = Object::GlobalContainer->new( notglobal => 1 ); | ||
| 51 | # | ||
| 52 | # $OC1->set('abc','123'); | ||
| 53 | # $OC2->set('abc','456'); | ||
| 54 | # | ||
| 55 | # print $OC1->get('abc'); ## returns 123 ! | ||
| 56 | # print $OC2->get('abc'); ## returns 456 ! | ||
| 57 | # | ||
| 58 | # Both instances act as them self, as own separate object. | ||
| 59 | # | ||
| 60 | # | ||
| 61 | # imported function | ||
| 62 | # ================= | ||
| 63 | # | ||
| 64 | # The most elegant way to deal with that global container, is to use a local funtion: | ||
| 65 | # | ||
| 66 | # use Object::GlobalContainer 'objcon'; | ||
| 67 | # | ||
| 68 | # Object::GlobalContainer->new(); | ||
| 69 | # | ||
| 70 | # objcon->set('abc','123'); | ||
| 71 | # objcon->get('abc'); | ||
| 72 | # | ||
| 73 | # Here it installs the function 'objcon' locally to access the container. Combined with the default behaviour as | ||
| 74 | # singleton, it is quite usefull. Please note, the name of the method you can choose by yourself and may be different | ||
| 75 | # in every class. | ||
| 76 | # | ||
| 77 | # Do not forget to call the constructor first! | ||
| 78 | # | ||
| 79 | # | ||
| 80 | # | ||
| 81 | # hash storage | ||
| 82 | # ============ | ||
| 83 | # | ||
| 84 | # To be honest, I was not lucky with the plain store other classes provide. What means, to have only one string as identifier. | ||
| 85 | # Often you have already hash structures and you want to store them, but maybe read them with one simple command and not two. | ||
| 86 | # | ||
| 87 | # The classical way, other object container usually work: | ||
| 88 | # | ||
| 89 | # my $data = { | ||
| 90 | # foo => { | ||
| 91 | # bar => { | ||
| 92 | # more => '123', | ||
| 93 | # } | ||
| 94 | # } | ||
| 95 | # }; | ||
| 96 | # | ||
| 97 | # store->set('some/where',$data); | ||
| 98 | # | ||
| 99 | # print store->get('some/where')->{'foo'}->{'bar'}->{'more'}; | ||
| 100 | # | ||
| 101 | # Here you had to use the get() plus the hash syntax to access the content. With Object::GlobalContainer it is easier to access | ||
| 102 | # the content directly: | ||
| 103 | # | ||
| 104 | # print store->get('some/where/foo/bar/more'); | ||
| 105 | # | ||
| 106 | # Because the used string (path) is realy a path for a hash structure. | ||
| 107 | # | ||
| 108 | # delimiter | ||
| 109 | # ========= | ||
| 110 | # 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: | ||
| 111 | # | ||
| 112 | # my $OC = Object::GlobalContainer->new( delimiter => '.' ); | ||
| 113 | # $OC->get('a.b.c'); | ||
| 114 | # | ||
| 115 | # If using the imported function access method, you can change the delimter via a method call: | ||
| 116 | # | ||
| 117 | # use Object::GlobalContainer 'objcon'; | ||
| 118 | # | ||
| 119 | # objcon->delimiter('.'); | ||
| 120 | # objcon->set('a.b.c','123'); | ||
| 121 | # objcon->get('a.b.c'); | ||
| 122 | # | ||
| 123 | # | ||
| 124 | # LICENSE | ||
| 125 | # ======= | ||
| 126 | # You can redistribute it and/or modify it under the conditions of LGPL. | ||
| 127 | # | ||
| 128 | # AUTHOR | ||
| 129 | # ====== | ||
| 130 | # Andreas Hernitscheck ahernit(AT)cpan.org | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 137 | 11 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 138 | 11 | ▶ | use Class::Inspector; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 139 | 0 | ▶ | use Hash::Work qw(merge_hashes); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 140 | |||
| 141 | our $VERSION='0.07'; | ||
| 142 | |||
| 143 | # Don't use that method! It is not a method but used by perl when | ||
| 144 | # this class is included to export a function in current namespace. | ||
| 145 | sub import { | ||
| 146 | my $pkg = shift; | ||
| 147 | my $exportfunc = shift || 'objcon'; | ||
| 148 | my $caller = caller; | ||
| 149 | |||
| 150 | require Exporter::AutoClean; | ||
| 151 | |||
| 152 | ## prepare the given name '$exportfunc' to return | ||
| 153 | ## the singleton object stored in the package. | ||
| 154 | my %exports = ( | ||
| 155 | "$exportfunc" => sub { return $__PACKAGE__::SINGLETON }, | ||
| 156 | ); | ||
| 157 | |||
| 158 | |||
| 159 | ## installs the function $exportfunc in the calling class | ||
| 160 | Exporter::AutoClean->export( $caller, %exports ); | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | # sets the name of the store area. | ||
| 167 | # Normaly you do not need to change that. | ||
| 168 | # Default is 'default'. Do not use reserved keys, | ||
| 169 | # it uses the name space of the module directly. | ||
| 170 | has 'storename' => ( | ||
| 171 | is => 'rw', | ||
| 172 | isa => 'Str', | ||
| 173 | default => 'default', | ||
| 174 | ); | ||
| 175 | |||
| 176 | |||
| 177 | # delimter for a path notation to store values. | ||
| 178 | has 'delimiter' => ( | ||
| 179 | is => 'rw', | ||
| 180 | isa => 'Str', | ||
| 181 | default => '/', | ||
| 182 | ); | ||
| 183 | |||
| 184 | |||
| 185 | has 'notglobal' => ( | ||
| 186 | is => 'rw', | ||
| 187 | isa => 'Int', | ||
| 188 | default => 0, | ||
| 189 | ); | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | ## Moose's constructor replace | ||
| 195 | sub BUILD { | ||
| 196 | my $this=shift; | ||
| 197 | |||
| 198 | if ( !$this->notglobal ){ | ||
| 199 | |||
| 200 | ## links to a global store by package name | ||
| 201 | ## or define a new. | ||
| 202 | $this->{'STORE'} = $__PACKAGE__::STORE || {}; | ||
| 203 | |||
| 204 | ## save, if it is new | ||
| 205 | $__PACKAGE__::STORE = $this->{'STORE'}; | ||
| 206 | |||
| 207 | $__PACKAGE__::SINGLETON = $this; | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | # if not global | ||
| 212 | $this->{'STORE'}||={}; | ||
| 213 | |||
| 214 | $this->{'COMMENTS'}||={}; | ||
| 215 | |||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | # Adds an object (any value or reference). | ||
| 221 | # Expects a key and a value. | ||
| 222 | # The key may be a path with slash delimiter. | ||
| 223 | # It will be stored as nested hash nodes. | ||
| 224 | # | ||
| 225 | # Alternatively you can use key,value,comment. | ||
| 226 | # And comment is a string describing what you store here. | ||
| 227 | # with get_comments() you can retreive the list (hash) of comments by key. | ||
| 228 | # | ||
| 229 | # OC->set('person/firstname','Peter','The name of a person'); | ||
| 230 | # | ||
| 231 | sub set { # void ($key1,$value1,$key2,$value2) | ||
| 232 | my $this=shift; | ||
| 233 | my $pre_key=shift; | ||
| 234 | my $pre_value=shift; | ||
| 235 | my $pre_comment=shift; | ||
| 236 | my $pre_4th=shift; | ||
| 237 | |||
| 238 | my @pairs=@_; # rest | ||
| 239 | |||
| 240 | if ($pre_4th){ # normal pairs | ||
| 241 | |||
| 242 | unshift @pairs, $pre_4th; | ||
| 243 | unshift @pairs, $pre_comment; | ||
| 244 | unshift @pairs, $pre_value; | ||
| 245 | unshift @pairs, $pre_key; | ||
| 246 | |||
| 247 | }else{ # with comment | ||
| 248 | |||
| 249 | unshift @pairs, $pre_value; | ||
| 250 | unshift @pairs, $pre_key; | ||
| 251 | |||
| 252 | $this->{'COMMENTS'}->{ $pre_key } = $pre_comment; | ||
| 253 | |||
| 254 | } | ||
| 255 | |||
| 256 | my %pairs=@pairs; | ||
| 257 | |||
| 258 | |||
| 259 | foreach my $key ( keys %pairs ) { | ||
| 260 | |||
| 261 | ## location holds the direct reference to the final store (right element of the tree) | ||
| 262 | my $location = $this->_hash_location_by_path( path => $key ); | ||
| 263 | |||
| 264 | my $value = $pairs{$key}; | ||
| 265 | |||
| 266 | # stores value to location | ||
| 267 | ${ $location } = $value; | ||
| 268 | |||
| 269 | # store for comments | ||
| 270 | $this->{"last_key_used"} = $key; | ||
| 271 | |||
| 272 | } | ||
| 273 | |||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | # Can merge a given hash to an existing hash in the object container. | ||
| 278 | # | ||
| 279 | sub merge { # void ($path,\%hashref,$comment) | ||
| 280 | my $this=shift; | ||
| 281 | my $path=shift; | ||
| 282 | my $hash=shift; | ||
| 283 | my $comment=shift; | ||
| 284 | |||
| 285 | my $ex = $this->get( $path ) || {}; | ||
| 286 | |||
| 287 | my $merged = merge_hashes( $ex, $hash ); | ||
| 288 | |||
| 289 | $this->set( $path, $merged, $comment ) | ||
| 290 | |||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | # Leave a comment for a key. You can do this e.g. after using class() | ||
| 296 | # | ||
| 297 | # OC->class('/foo/bar','Foo::Bar')->run(); | ||
| 298 | # OC->comment('/foo/bar',"This is class Bar"); | ||
| 299 | # | ||
| 300 | # If no key is given, it uses the last used set() path. Do not use this | ||
| 301 | # if you do multithreading, because you cant ensure which was the last entry. | ||
| 302 | # | ||
| 303 | # OC->class('/foo/bar','Foo::Bar')->run(); | ||
| 304 | # OC->comment("This is class Bar"); | ||
| 305 | # | ||
| 306 | sub comment { # void ($key,$description) | ||
| 307 | my $this=shift; | ||
| 308 | my $pre_key=shift; | ||
| 309 | my $pre_comment=shift; | ||
| 310 | |||
| 311 | if (!$pre_comment){ | ||
| 312 | $pre_comment = $pre_key; | ||
| 313 | $pre_key = $this->{"last_key_used"}; | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | $this->{'COMMENTS'}->{ $pre_key } = $pre_comment; | ||
| 319 | |||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | # With add() it is possible to add it to an array. If at the | ||
| 324 | # target is no array, it will be added automatically. | ||
| 325 | sub add { # void ($path,$value) | ||
| 326 | my $this = shift; | ||
| 327 | my $path = shift; | ||
| 328 | my $value = shift; | ||
| 329 | |||
| 330 | my $entry = $this->get( $path ); | ||
| 331 | |||
| 332 | $entry ||= []; | ||
| 333 | |||
| 334 | push @$entry, $value; | ||
| 335 | |||
| 336 | $this->set( $path, $entry ); | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | # Returns the hash with comments to keys. Keys without a comment won't be returned. | ||
| 344 | # | ||
| 345 | # use Data::Dumpers | ||
| 346 | # print Dumper(OC->get_comments_hash); | ||
| 347 | # | ||
| 348 | sub get_comments_hash { # HASH | ||
| 349 | my $this = shift; | ||
| 350 | |||
| 351 | return $this->{'COMMENTS'}; | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | # builds a recursive hash reference by given path. | ||
| 358 | # takes hash values like: | ||
| 359 | # location = reference to formal hashnode | ||
| 360 | # path = a path like abc/def/more | ||
| 361 | sub _hash_location_by_path { | ||
| 362 | my $this = shift; | ||
| 363 | my $v={@_}; | ||
| 364 | my $path = $v->{'path'} || ''; | ||
| 365 | my $exec_last = $v->{'exec_last'}; | ||
| 366 | my $storename = $this->storename(); | ||
| 367 | my $dont_create_undef_entry = $v->{'dont_create_undef_entry'}; | ||
| 368 | my $location = $v->{'location'} || \$this->{'STORE'}->{ $storename }; # ref to a location | ||
| 369 | my $pathlocation; | ||
| 370 | |||
| 371 | my $delim = $this->delimiter(); | ||
| 372 | |||
| 373 | ## remove beginning slash | ||
| 374 | if (index($path,$delim) == 0){ | ||
| 375 | $path=~ s|^.||; | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | my @path = split( /$delim/, $path ); | ||
| 381 | if (scalar(@path) == 0){ die "path has to less elements" }; | ||
| 382 | |||
| 383 | my $first = shift @path; # takes first and shorten path | ||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | if ( scalar( @path ) ){ # more path elements? | ||
| 388 | |||
| 389 | $pathlocation = \${ $location }->{ $first }; | ||
| 390 | |||
| 391 | # recursive step down the path | ||
| 392 | $pathlocation = $this->_hash_location_by_path( path => join($delim,@path), | ||
| 393 | location => $pathlocation, | ||
| 394 | remove => $v->{'remove'}, | ||
| 395 | exec_last => $exec_last, | ||
| 396 | dont_create_undef_entry => $dont_create_undef_entry, | ||
| 397 | ); | ||
| 398 | |||
| 399 | }else{ # last path element? | ||
| 400 | |||
| 401 | if ($v->{'remove'}){ | ||
| 402 | delete ${ $location }->{ $first }; | ||
| 403 | $dont_create_undef_entry = 1; | ||
| 404 | } | ||
| 405 | |||
| 406 | if ($exec_last){ | ||
| 407 | &$exec_last( location => $location, key => $first ); | ||
| 408 | } | ||
| 409 | |||
| 410 | ## same line again. it seems to be one too much, but it isnt, | ||
| 411 | ## that line creates also an undef value, that exists what | ||
| 412 | ## changes the data. exec subs may work different. | ||
| 413 | if ( !$dont_create_undef_entry ){ | ||
| 414 | $pathlocation = \${ $location }->{ $first }; | ||
| 415 | } | ||
| 416 | |||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | return $pathlocation; | ||
| 421 | } | ||
| 422 | |||
| 423 | |||
| 424 | |||
| 425 | |||
| 426 | # returns a value by given key, which can be a path. | ||
| 427 | sub get { # scalar|reference ($path) | ||
| 428 | my $this=shift; | ||
| 429 | my $key=shift; | ||
| 430 | |||
| 431 | my $location = $this->_hash_location_by_path( path => $key ); | ||
| 432 | |||
| 433 | return ${ $location }; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | # deletes an entry by removing it from the hash | ||
| 438 | sub delete { # void ($path) | ||
| 439 | my $this=shift; | ||
| 440 | my $path=shift; | ||
| 441 | |||
| 442 | my $location = $this->_hash_location_by_path( path => $path, remove => 1 ); | ||
| 443 | |||
| 444 | |||
| 445 | |||
| 446 | } | ||
| 447 | |||
| 448 | |||
| 449 | # Validates if an entry (key) exists. | ||
| 450 | sub exists { # $boolean ($path) | ||
| 451 | my $this=shift; | ||
| 452 | my $path=shift; | ||
| 453 | |||
| 454 | my $ex; | ||
| 455 | |||
| 456 | my $location = $this->_hash_location_by_path( | ||
| 457 | path => $path, | ||
| 458 | dont_create_undef_entry => 1, | ||
| 459 | exec_last => sub { | ||
| 460 | |||
| 461 | my $v={@_}; | ||
| 462 | my $location = $v->{'location'}; | ||
| 463 | my $key = $v->{'key'}; | ||
| 464 | |||
| 465 | $ex = exists ${ $location }->{ $key }; | ||
| 466 | |||
| 467 | } # end sub | ||
| 468 | |||
| 469 | ); | ||
| 470 | |||
| 471 | |||
| 472 | |||
| 473 | return $ex; | ||
| 474 | } | ||
| 475 | |||
| 476 | |||
| 477 | |||
| 478 | # Creates an instance of the given class and sets it to the given path. | ||
| 479 | # it also returns the new object. It is managed as a singleton, what means, | ||
| 480 | # if there is already an object in the given path, it won't instantiate the | ||
| 481 | # class again. It will call the constructor of the class with given | ||
| 482 | # parameters. | ||
| 483 | # If it is already loaded onto given path, it won't load it again. If you want to | ||
| 484 | # force a new load, please delete it first or replace it with set(). | ||
| 485 | # | ||
| 486 | # my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456); | ||
| 487 | # | ||
| 488 | # I think the way Object::Container register classes, by it's own name, is against the | ||
| 489 | # idea of an object container. Because the getting part should not need to know | ||
| 490 | # what exactly has been set, to keep the application flexible. | ||
| 491 | # That is why I am setting it into the given path and not the classname. | ||
| 492 | # | ||
| 493 | # If for example dealing with mod_perl and keeping the application in memory, it | ||
| 494 | # is very nice to re-run that code, but automatically skipping a new instantiation. | ||
| 495 | # | ||
| 496 | # What means that that line: | ||
| 497 | # | ||
| 498 | # my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456); | ||
| 499 | # | ||
| 500 | # will be executed in a different way when running it the second time, if it still is in memory. | ||
| 501 | # Then it does not instantiate the class again, but just returns the existing instance. | ||
| 502 | # | ||
| 503 | # Otherwise you would need to build your own complex if/else line. | ||
| 504 | # | ||
| 505 | sub class { # $classobject ($path, $classname, %classparam) | ||
| 506 | my $this=shift; | ||
| 507 | my $path=shift; | ||
| 508 | my $classname = shift or die "no classname given"; | ||
| 509 | my %param = @_; | ||
| 510 | |||
| 511 | ## is class installed? | ||
| 512 | if (! Class::Inspector->installed($classname) ){ | ||
| 513 | die "Class \'$classname\' is not installed on this system or wrong lib path."; | ||
| 514 | } | ||
| 515 | |||
| 516 | ## load the class | ||
| 517 | if (! Class::Inspector->loaded($classname) ){ | ||
| 518 | |||
| 519 | eval "use $classname"; ## no critic | ||
| 520 | |||
| 521 | if ( $@ ){ | ||
| 522 | die "error loading class \'$classname\' by require: $@"; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | ## check if it is already at path, if yes, return that (singleton) | ||
| 527 | if ( ref($this->get($path)) eq $classname ){ | ||
| 528 | return $this->get($path); | ||
| 529 | } | ||
| 530 | |||
| 531 | my $instance = $classname->new(%param); | ||
| 532 | if ( !$instance ){ | ||
| 533 | die "error loading class \'$classname\' by calling constructor $!"; | ||
| 534 | } | ||
| 535 | |||
| 536 | $this->set($path, $instance); | ||
| 537 | |||
| 538 | |||
| 539 | return $instance; | ||
| 540 | } | ||
| 541 | |||
| 542 | |||
| 543 | |||
| 544 | 1; | ||
| 545 | |||
| 546 | |||
| 547 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||
| 548 | |||
| 549 | =head1 NAME | ||
| 550 | |||
| 551 | Object::GlobalContainer - A global singleton object container | ||
| 552 | |||
| 553 | |||
| 554 | =head1 SYNOPSIS | ||
| 555 | |||
| 556 | |||
| 557 | use Object::GlobalContainer; | ||
| 558 | |||
| 559 | my $OC = Object::GlobalContainer->new(); | ||
| 560 | |||
| 561 | $OC->set('abc','123'); | ||
| 562 | $OC->set('first/second/third','456'); | ||
| 563 | |||
| 564 | print $OC->get('abc'); | ||
| 565 | print $OC->get('first/second/third'); | ||
| 566 | |||
| 567 | |||
| 568 | ## per default it works as singleton! | ||
| 569 | use Object::GlobalContainer; | ||
| 570 | |||
| 571 | my $OC1 = Object::GlobalContainer->new(); | ||
| 572 | my $OC2 = Object::GlobalContainer->new(); | ||
| 573 | |||
| 574 | $OC1->set('abc','123'); | ||
| 575 | print $OC2->get('abc'); ## returns also 123 ! | ||
| 576 | |||
| 577 | # to disable singleton behaviour, write: | ||
| 578 | my $OC = Object::GlobalContainer->new( notglobal => 1 ); | ||
| 579 | |||
| 580 | # loading a class | ||
| 581 | my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456); | ||
| 582 | |||
| 583 | |||
| 584 | |||
| 585 | |||
| 586 | =head1 DESCRIPTION | ||
| 587 | |||
| 588 | The GlobalContainer is another object container and very similar to Object::Container and Object::Registrar, but | ||
| 589 | it supports deep hash storages. It means you can save your data by using a path notation like 'foo/bar/more'. | ||
| 590 | And that is not handled as a string, but a hash. | ||
| 591 | |||
| 592 | |||
| 593 | |||
| 594 | =head1 REQUIRES | ||
| 595 | |||
| 596 | L<Class::Inspector> | ||
| 597 | |||
| 598 | L<Moose> | ||
| 599 | |||
| 600 | |||
| 601 | =head1 METHODS | ||
| 602 | |||
| 603 | |||
| 604 | |||
| 605 | =head2 class | ||
| 606 | |||
| 607 | my $classobject = class($path, $classname, %classparam); | ||
| 608 | |||
| 609 | Creates an instance of the given class and sets it to the given path. | ||
| 610 | it also returns the new object. It is managed as a singleton, what means, | ||
| 611 | if there is already an object in the given path, it won't instantiate the | ||
| 612 | class again. It will call the constructor of the class with given | ||
| 613 | parameters. | ||
| 614 | If it is already loaded onto given path, it won't load it again. If you want to | ||
| 615 | force a new load, please delete it first or replace it with set(). | ||
| 616 | |||
| 617 | my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456); | ||
| 618 | |||
| 619 | I think the way Object::Container register classes, by it's own name, is against the | ||
| 620 | idea of an object container. Because the getting part should not need to know | ||
| 621 | what exactly has been set, to keep the application flexible. | ||
| 622 | That is why I am setting it into the given path and not the classname. | ||
| 623 | |||
| 624 | If for example dealing with mod_perl and keeping the application in memory, it | ||
| 625 | is very nice to re-run that code, but automatically skipping a new instantiation. | ||
| 626 | |||
| 627 | What means that that line: | ||
| 628 | |||
| 629 | my $c = $OC->class('classes/database', 'Local::MyDatabaseHandler', foo => 123, bar => 456); | ||
| 630 | |||
| 631 | will be executed in a different way when running it the second time, if it still is in memory. | ||
| 632 | Then it does not instantiate the class again, but just returns the existing instance. | ||
| 633 | |||
| 634 | Otherwise you would need to build your own complex if/else line. | ||
| 635 | |||
| 636 | |||
| 637 | |||
| 638 | =head2 delete | ||
| 639 | |||
| 640 | delete($path); | ||
| 641 | |||
| 642 | deletes an entry by removing it from the hash | ||
| 643 | |||
| 644 | |||
| 645 | =head2 exists | ||
| 646 | |||
| 647 | my $boolean = exists($path); | ||
| 648 | |||
| 649 | Validates if an entry (key) exists. | ||
| 650 | |||
| 651 | |||
| 652 | =head2 get | ||
| 653 | |||
| 654 | my $scalar | reference = get($path); | ||
| 655 | |||
| 656 | returns a value by given key, which can be a path. | ||
| 657 | |||
| 658 | |||
| 659 | |||
| 660 | =head2 set | ||
| 661 | |||
| 662 | set($key1, $value1, $key2, $value2); | ||
| 663 | |||
| 664 | Adds an object (any value or reference). | ||
| 665 | Expects a key and a value. | ||
| 666 | The key may be a path with slash delimiter. | ||
| 667 | It will be stored as nested hash nodes. | ||
| 668 | |||
| 669 | |||
| 670 | |||
| 671 | =head1 hash storage | ||
| 672 | |||
| 673 | |||
| 674 | To be honest, I was not lucky with the plain store other classes provide. What means, to have only one string as identifier. | ||
| 675 | Often you have already hash structures and you want to store them, but maybe read them with one simple command and not two. | ||
| 676 | |||
| 677 | The classical way, other object container usually work: | ||
| 678 | |||
| 679 | my $data = { | ||
| 680 | foo => { | ||
| 681 | bar => { | ||
| 682 | more => '123', | ||
| 683 | } | ||
| 684 | } | ||
| 685 | }; | ||
| 686 | |||
| 687 | store->set('some/where',$data); | ||
| 688 | |||
| 689 | print store->get('some/where')->{'foo'}->{'bar'}->{'more'}; | ||
| 690 | |||
| 691 | Here you had to use the get() plus the hash syntax to access the content. With Object::GlobalContainer it is easier to access | ||
| 692 | the content directly: | ||
| 693 | |||
| 694 | print store->get('some/where/foo/bar/more'); | ||
| 695 | |||
| 696 | Because the used string (path) is realy a path for a hash structure. | ||
| 697 | |||
| 698 | |||
| 699 | |||
| 700 | =head1 singleton | ||
| 701 | |||
| 702 | As shown in synopsis's example, the default behaviour is to work as singleton, because you usually want to | ||
| 703 | share things globaly. Disable that by using the flag 'notglobal' in the constructor. | ||
| 704 | |||
| 705 | |||
| 706 | |||
| 707 | |||
| 708 | =head1 singleton, off (notglobal) | ||
| 709 | |||
| 710 | If you want to disable the singleton behaviour, you can use 'notglobal' to do so: | ||
| 711 | |||
| 712 | my $OC1 = Object::GlobalContainer->new( notglobal => 1 ); | ||
| 713 | my $OC2 = Object::GlobalContainer->new( notglobal => 1 ); | ||
| 714 | |||
| 715 | $OC1->set('abc','123'); | ||
| 716 | $OC2->set('abc','456'); | ||
| 717 | |||
| 718 | print $OC1->get('abc'); ## returns 123 ! | ||
| 719 | print $OC2->get('abc'); ## returns 456 ! | ||
| 720 | |||
| 721 | Both instances act as them self, as own separate object. | ||
| 722 | |||
| 723 | |||
| 724 | |||
| 725 | |||
| 726 | |||
| 727 | =head1 imported function | ||
| 728 | |||
| 729 | |||
| 730 | The most elegant way to deal with that global container, is to use a local funtion: | ||
| 731 | |||
| 732 | use Object::GlobalContainer 'objcon'; | ||
| 733 | |||
| 734 | Object::GlobalContainer->new(); | ||
| 735 | |||
| 736 | objcon->set('abc','123'); | ||
| 737 | objcon->get('abc'); | ||
| 738 | |||
| 739 | Here it installs the function 'objcon' locally to access the container. Combined with the default behaviour as | ||
| 740 | singleton, it is quite usefull. Please note, the name of the method you can choose by yourself and may be different | ||
| 741 | in every class. | ||
| 742 | |||
| 743 | Do not forget to call the constructor first! | ||
| 744 | |||
| 745 | |||
| 746 | |||
| 747 | |||
| 748 | |||
| 749 | =head1 delimiter | ||
| 750 | |||
| 751 | 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: | ||
| 752 | |||
| 753 | my $OC = Object::GlobalContainer->new( delimiter => '.' ); | ||
| 754 | $OC->get('a.b.c'); | ||
| 755 | |||
| 756 | If using the imported function access method, you can change the delimter via a method call: | ||
| 757 | |||
| 758 | use Object::GlobalContainer 'objcon'; | ||
| 759 | |||
| 760 | objcon->delimiter('.'); | ||
| 761 | objcon->set('a.b.c','123'); | ||
| 762 | objcon->get('a.b.c'); | ||
| 763 | |||
| 764 | |||
| 765 | |||
| 766 | |||
| 767 | |||
| 768 | |||
| 769 | =head1 AUTHOR | ||
| 770 | |||
| 771 | Andreas Hernitscheck ahernit(AT)cpan.org | ||
| 772 | |||
| 773 | |||
| 774 | =head1 LICENSE | ||
| 775 | |||
| 776 | You can redistribute it and/or modify it under the conditions of LGPL. | ||
| 777 | |||
| 778 | |||
| 779 | |||
| 780 | =cut | ||
| 781 | |||