package Aspect::Library::Singleton; use strict; use warnings; use Aspect::Modular (); use Aspect::Advice::Before (); use Aspect::Pointcut::Call (); our $VERSION = '1.02'; our @ISA = 'Aspect::Modular'; my %CACHE = (); sub get_advice { my $self = shift; Aspect::Advice::Around->new( lexical => $self->lexical, pointcut => Aspect::Pointcut::Call->new(shift), code => sub { my $class = $_->self; $class = ref $class || $class; if ( exists $CACHE{$class} ) { $_->return_value($CACHE{$class}); } else { $_->proceed; $CACHE{$class} = $_->return_value; } }, ); } 1; __END__ =pod =head1 NAME Aspect::Library::Singleton - A singleton aspect =head1 SYNOPSIS use Aspect; use Aspect::Singleton; aspect Singleton => 'Foo::new'; my $f1 = Foo->new; my $f2 = Foo->new; # Both $f1 and $f2 refer to the same object =head1 DESCRIPTION A reusable aspect that forces singleton behavior on a constructor. The constructor is defined by a pointcut spec: a string. regexp, or code ref. It is slightly different from C (L): =over =item * No specific name requirement on the constructor for the external interface, or for the implementation (C requires clients use C, and that subclasses override C<_new_instance()>). With aspects, you can change the cardinality of your objects without changing the clients, or the objects themselves. =item * No need to inherit from anything- use pointcuts to specify the constructors you want to memoize. Instead of I singleton behavior from a base class, you are I it in, using the aspect. =item * No package variable or method is added to the callers namespace =back Note that this is just a special case of memoizing. =head1 AUTHORS Adam Kennedy Eadamk@cpan.orgE Marcel GrEnauer Emarcel@cpan.orgE Ran Eilam Eeilara@cpan.orgE =head1 COPYRIGHT Copyright 2001 by Marcel GrEnauer Some parts copyright 2009 - 2012 Adam Kennedy. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut