Collection::Utl::LazyObject - Lazy call.


Collection documentation Contained in the Collection distribution.

Index


Code Index:

NAME

Top

Collection::Utl::LazyObject - Lazy call.

SYNOPSIS

Top

    use Collection::Object;
    my $lazy = new Collection::Utl::LazyObject:: 
            sub { new SomeClass:: %attr };

DESCRIPTION

Top

Lazy call.

SEE ALSO

Top

Collection::ActiveRecord, Collection, README

AUTHOR

Top

Zahatski Aliaksandr, <zag@cpan.org>

COPYRIGHT AND LICENSE

Top


Collection documentation Contained in the Collection distribution.
package Collection::Utl::LazyObject;

use strict;
use warnings;
use strict;
use Carp;
$Collection::Utl::LazyObject::VERSION = '0.01';
no strict 'refs';
### install get/set accessors for this object.
for my $key (qw/   ___sub_ref___  ___obj_ref___ /) {
    no strict 'refs';
    *{ __PACKAGE__ . "::$key" } = sub {
        my $self = shift;
        $self->{$key} = $_[0] if @_;
        return $self->{$key};
      }
}

sub new {
    my $class = shift;
    $class = ref $class if ref $class;
    my $self = bless( {}, $class );
    $self->___sub_ref___(shift) || return;
    $self;
}

sub ___get_object___ {
    my $self = shift;
    my $obj  = $self->___obj_ref___;
    unless ($obj) {
        $obj = $self->___sub_ref___->()
          || die "can't do lazy call. need result";
        $self->___obj_ref___($obj);
    }
    $obj;
}

sub AUTOLOAD {
    my $self = shift;
    return if $Collection::Utl::LazyObject::AUTOLOAD =~ /::(DESTROY)$/;
    ( my $auto_sub ) = $Collection::Utl::LazyObject::AUTOLOAD =~ /.*::(.*)/;
    return $self->___get_object___->$auto_sub(@_);

}

1;
__END__