/usr/share/perl5/Object/Container.pm is in libobject-container-perl 0.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | package Object::Container;
use strict;
use warnings;
use parent qw(Class::Accessor::Fast);
use Carp;
our $VERSION = '0.14';
__PACKAGE__->mk_accessors(qw/registered_classes autoloader_rules objects/);
BEGIN {
our $_HAVE_EAC = 1;
eval { local $SIG{__DIE__}; require Exporter::AutoClean; };
if ($@) {
$_HAVE_EAC = 0;
}
}
do {
my @EXPORTS;
sub import {
my ($class, $name) = @_;
return unless $name;
my $caller = caller;
{
no strict 'refs';
if ($name =~ /^-base$/i) {
push @{"${caller}::ISA"}, $class;
my $r = $class->can('register');
my $l = $class->can('autoloader');
my %exports = (
register => sub { $r->($caller, @_) },
autoloader => sub { $l->($caller, @_) },
preload => sub {
$caller->instance->get($_) for @_;
},
preload_all_except => sub {
$caller->instance->load_all_except(@_);
},
preload_all => sub {
$caller->instance->load_all;
},
);
if ($Object::Container::_HAVE_EAC) {
Exporter::AutoClean->export( $caller, %exports );
}
else {
while (my ($name, $fn) = each %exports) {
*{"${caller}::${name}"} = $fn;
}
@EXPORTS = keys %exports;
}
}
else {
no strict 'refs';
*{"${caller}::${name}"} = sub {
my ($target) = @_;
return $target ? $class->get($target) : $class;
};
}
}
}
sub unimport {
my $caller = caller;
no strict 'refs';
for my $name (@EXPORTS) {
delete ${ $caller . '::' }{ $name };
}
1; # for EOF
}
};
my %INSTANCES;
sub instance {
my $class = shift;
return $INSTANCES{$class} ||= $class->new;
}
sub has_instance {
my $class = shift;
$class = ref $class || $class;
return $INSTANCES{$class};
};
sub new {
$_[0]->SUPER::new( +{
registered_classes => +{},
autoloader_rules => +[],
objects => +{},
} );
}
sub register {
my ($self, $args, @rest) = @_;
$self = $self->instance unless ref $self;
my ($class, $initializer, $is_preload);
if (defined $args && !ref $args) {
$class = $args;
if (@rest == 1 and ref $rest[0] eq 'CODE') {
$initializer = $rest[0];
}
else {
$initializer = sub {
$self->ensure_class_loaded($class);
$class->new(@rest);
};
}
}
elsif (ref $args eq 'HASH') {
$class = $args->{class};
$args->{args} ||= [];
if (ref $args->{initializer} eq 'CODE') {
$initializer = $args->{initializer};
}
else {
$initializer = sub {
$self->ensure_class_loaded($class);
$class->new(@{$args->{args}});
};
}
$is_preload = 1 if $args->{preload};
}
else {
croak "Usage: $self->register($class || { class => $class ... })";
}
$self->registered_classes->{$class} = $initializer;
$self->get($class) if $is_preload;
return $initializer;
}
sub unregister {
my ($self, $class) = @_;
$self = $self->instance unless ref $self;
delete $self->registered_classes->{$class} and $self->remove($class);
}
sub autoloader {
my ($self, $rule, $trigger) = @_;
$self = $self->instance unless ref $self;
push @{ $self->autoloader_rules }, [$rule, $trigger];
}
sub get {
my ($self, $class) = @_;
$self = $self->instance unless ref $self;
my $obj = $self->objects->{ $class } ||= do {
my $initializer = $self->registered_classes->{ $class };
$initializer ? $initializer->($self) : ();
};
unless ($obj) {
# autoloaderer
if (my ($trigger) = grep { $class =~ /$_->[0]/ } @{ $self->autoloader_rules }) {
$trigger->[1]->($self, $class);
}
$obj = $self->objects->{ $class } ||= do {
my $initializer = $self->registered_classes->{ $class };
$initializer ? $initializer->($self) : ();
};
}
$obj or croak qq["$class" is not registered in @{[ ref $self ]}];
}
sub remove {
my ($self, $class) = @_;
$self = $self->instance unless ref $self;
delete $self->objects->{ $class };
}
sub load_all {
my ($self) = @_;
$self->load_all_except;
}
sub load_all_except {
my ($self, @except) = @_;
$self = $self->instance unless ref $self;
for my $class (keys %{ $self->registered_classes }) {
next if grep { $class eq $_ } @except;
$self->get($class);
}
}
# taken from Mouse
sub _is_class_loaded {
my $class = shift;
return 0 if ref($class) || !defined($class) || !length($class);
# walk the symbol table tree to avoid autovififying
# \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
my $pack = \%::;
foreach my $part (split('::', $class)) {
$part .= '::';
return 0 if !exists $pack->{$part};
my $entry = \$pack->{$part};
return 0 if ref($entry) ne 'GLOB';
$pack = *{$entry}{HASH};
}
return 0 if !%{$pack};
# check for $VERSION or @ISA
return 1 if exists $pack->{VERSION}
&& defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
return 1 if exists $pack->{ISA}
&& defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
# check for any method
foreach my $name( keys %{$pack} ) {
my $entry = \$pack->{$name};
return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
}
# fail
return 0;
}
sub _try_load_one_class {
my $class = shift;
return '' if _is_class_loaded($class);
my $klass = $class;
$klass =~ s{::}{/}g;
$klass .= '.pm';
return do {
local $@;
eval { require $klass };
$@;
};
}
sub ensure_class_loaded {
my ($self, $class) = @_;
my $e = _try_load_one_class($class);
Carp::confess "Could not load class ($class) because : $e" if $e;
return $class;
}
1;
__END__
=for stopwords DSL OO runtime singletonize unregister preload
=head1 NAME
Object::Container - simple object container
=head1 SYNOPSIS
use Object::Container;
# initialize container
my $container = Object::Container->new;
# register class
$container->register('HTML::TreeBuilder');
# register class with initializer
$container->register('WWW::Mechanize', sub {
my $mech = WWW::Mechanize->new( stack_depth => 1 );
$mech->agent_alias('Windows IE 6');
return $mech;
});
# get object
my $mech = $container->get('WWW::Mechanize');
# also available Singleton interface
my $container = Object::Container->instance;
# With singleton interface, you can use register/get method as class method
Object::Container->register('WWW::Mechanize');
my $mech = Object::Container->get('WWW::Mechanize');
# Export singleton interface
use Object::Container 'container';
container->register('WWW::Mechanize');
my $mech = container->get('WWW::Mechanize');
my $mech = container('WWW::Mechanize'); # save as above
# Subclassing singleton interface
package MyContainer;
use Object::Container '-base';
register mech => sub { WWW::Mechanize->new };
# use it
use MyContainer 'con';
con('mech')->get('http://example.com');
=head1 DESCRIPTION
This module is a object container interface which supports both OO interface and Singleton interface.
If you want to use one module from several places, you might use L<Class::Singleton> to access the module from any places. But you should subclass each modules to singletonize.
This module provide singleton container instead of module itself, so it is easy to singleton multiple classes.
L<Object::Registrar> is a similar module to this. But Object::Container has also OO interface and supports lazy initializer. (describing below)
=head2 OO and Singleton interfaces
This module provide two interfaces: OO and Singleton.
OO interface is like this:
my $container = Object::Container->new;
It is normal object oriented interface. And you can use multiple container at the same Time:
my $container1 = Object::Container->new;
my $container2 = Object::Container->new;
Singleton is also like this:
my $container = Object::Container->instance;
instance method always returns singleton object. With this interface, you can 'register' and 'get' method as class method:
Object::Container->register('WWW::Mechanize');
my $mech = Object::Container->get('WWW::Mechanize');
When you want use multiple container with Singleton interface, you have to create subclass like this:
MyContainer1->get('WWW::Mechanize');
MyContainer2->get('WWW::Mechanize');
=head2 Singleton interface with EXPORT function for lazy people
If you are lazy person, and don't want to write something long code like:
MyContainer->get('WWW::Mechanize');
This module provide export functions to shorten this.
If you use your container with function name, the function will be exported and act as container:
use MyContainer 'container';
container->register(...);
container->get(...);
container(...); # shortcut to ->get(...);
=head2 Subclassing singleton interface for lazy people
If you are lazy person, and don't want to write something long code in your subclass like:
__PACKAGE__->register( ... );
Instead of above, this module provide subclassing interface.
To do this, you need to write below code to subclass instead of C<use base>.
use Object::Container '-base';
And then you can register your object via DSL functions:
register ua => sub { LWP::UserAgent->new };
=head2 lazy loading and resolve dependencies
The object that is registered by 'register' method is not initialized until calling 'get' method.
Object::Container->register('WWW::Mechanize', sub { WWW::Mechanize->new }); # doesn't initialize here
my $mech = Object::Container->get('WWW::Mechanize'); # initialize here
This feature helps you to create less resource and fast runtime script in case of lots of object registered.
And you can resolve dependencies between multiple modules with Singleton interface.
For example:
Object::Container->register('HTTP::Cookies', sub { HTTP::Cookies->new( file => '/path/to/cookie.dat' ) });
Object::Container->register('LWP::UserAgent', sub {
my $cookies = Object::Container->get('HTTP::Cookies');
LWP::UserAgent->new( cookie_jar => $cookies );
});
You can resolve dependencies by calling 'get' method in initializer like above.
In that case, only LWP::UserAgent and HTTP::Cookies are initialized.
=head1 METHODS
=head2 new
Create new object.
=head2 instance
Create singleton object and return it.
=head2 register( $class, @args )
=head2 register( $class_or_name, $initialize_code )
=head2 register( { class => $class_or_name ... } )
Register classes to container.
Most simple usage is:
Object::Container->register('WWW::Mechanize');
First argument is class name to object. In this case, execute 'WWW::Mechanize->new' when first get method call.
Object::Container->register('WWW::Mechanize', @args );
is also execute 'WWW::Mechanize->new(@args)'.
If you use different constructor from 'new', want to custom initializer, or want to include dependencies, you can custom initializer to pass a coderef as second argument.
Object::Container->register('WWW::Mechanize', sub {
my $mech = WWW::Mechanize->new( stack_depth );
$mech->agent_alias('Windows IE 6');
return $mech;
});
This coderef (initialize) should return object to contain.
With last way you can pass any name to first argument instead of class name.
Object::Container->register('ua1', sub { LWP::UserAgent->new });
Object::Container->register('ua2', sub { LWP::UserAgent->new });
If you want to initialize and register at the same time, the following can.
Object::Container->register({ class => 'LWP::UserAgent', preload => 1 });
I<initializer> option can be specified.
Object::Container->register({ class => 'WWW::Mechanize', initializer => sub {
my $mech = WWW::Mechanize->new( stack_depth );
$mech->agent_alias('Windows IE 6');
return $mech;
}, preload => 1 });
This is the same as written below.
Object::Container->register('WWW::Mechanize', sub {
my $mech = WWW::Mechanize->new( stack_depth );
$mech->agent_alias('Windows IE 6');
return $mech;
});
Object::Container->get('WWW::Mechanize');
If you specify I<args> option is:
Object::Container->register({ class => 'LWP::UserAgent', args => \@args, preload => 1 });
It is, as you know, the same below.
Object::Container->register('LWP::UserAgent', @args);
Object::Container->get('LWP::UserAgent');
=head2 unregister($class_or_name)
Unregister classes from container.
=head2 get($class_or_name)
Get the object that registered by 'register' method.
First argument is same as 'register' method.
=head2 remove($class_or_name)
Remove the cached object that is created at C<get> method above.
Return value is the deleted object if it's exists.
=head2 ensure_class_loaded($class)
This is utility method that load $class if $class is not loaded.
It's useful when you want include dependency in initializer and want lazy load the modules.
=head2 load_all
=head2 load_all_except(@classes_or_names)
This module basically does lazy object initializations, but in some situation, for Copy-On-Write or for runtime speed for example, you might want to preload objects.
For the purpose C<load_all> and C<load_all_except> method are exists.
Object::Container->load_all;
This method is load all registered object at once.
Also if you have some objects that keeps lazy loading, do like following:
Object::Container->load_all_except(qw/Foo Bar/);
This means all objects except 'Foo' and 'Bar' are loaded.
=head1 EXPORT FUNCTIONS ON SUBCLASS INTERFACE
Same functions for C<load_all> and C<load_all_except> exists at subclass interface.
Below is list of these functions.
=head2 preload(@classes_or_names)
=head2 preload_all
=head2 preload_all_except
As predictable by name, C<preload_all> is equals to C<load_all> and C<preload_all_except> is equals to <load_all_except>.
=head1 SEE ALSO
L<Class::Singleton>, L<Object::Registrar>.
=head1 AUTHOR
Daisuke Murase <typester@cpan.org>
=head1 COPYRIGHT & LICENSE
Copyright (c) 2009 KAYAC Inc. All rights reserved.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
1;
|