Truly anonymous classes for Perl5
ARTISTIC-2.0 License
=head1 NAME
Class::Anonymous - Truly private classes with private data for Perl5
=head1 SYNOPSIS
use feature 'say'; use Class::Anonymous; use Class::Anonymous::Utils ':all';
my $lifeform = class { my ($self, $name) = @_; method greeting => sub { "My name is $name" }; };
my $mortal = extend $lifeform => via { my ($self, $name, $age) = @_; around greeting => sub { my $orig = shift; $orig->() . " and I'm $age years old"; }; };
my $bob = $mortal->new('Bob', 40); say $bob->greeting; say 'Bob is mortal' if $bob->isa($mortal); say 'Bob is a lifeform' if $bob->isa($lifeform);
=head1 DESCRIPTION
LClass::Anonymous implements anonymous classes and private data. This private data is just the lexical variables available during the builder callback(s) that are used to build the class. If LPackage::Anon is available, then no stash entry is created at all. If not, then the classes will actually be blessed into LClass::Anonymous::Instance though this is to be considered an implementation detail and subject to change.
The instance itself is a code reference which can be thought of as a meta-object. Called with a single string fetches the method of that name and returns it. Called with a string and a code reference attaches a new method to the object. Helper functions are provided in LClass::Anonymous::Utils which provides LClass::Anonymous::Utils/method and method modifiers for ease of use.
my $class = class { my ($self) = @_; $self->(mymethod => sub { ... }); my $mymethod = $self->('mymethod'); }
=head1 EXPORTED FUNCTIONS
=head2 class
my $class = class { my ($self) = @_; ... };
Define a new class. Takes a block (or code reference) which will be called to build and instance of that class. The callback is called with the new empty instance and any arguments passed to C. Note that subclasses might need more initialization arguments, so you might want to plan for that.
=head2 extend
my $subclass = extend $class, sub { my ($self) = @_; ... };
Define a new subclass of an existing anonymous class. Takes an existing class and a code reference which will be called after the parent class builder callback(s). Note that all callbacks receive the same arguments, so you might want to plan for that.
=head2 via
my $subclass = extend $class, via { my ($self) = @_; ... };
Sugar for defining a code reference as a block, simply to make L look better.
=head1 OTHER FUNCTIONS
=head2 instance
Builds a raw instance of a generic anonymous object. All arguments are the classes to be returned by C. By default it only fully implements the C, C and C methods. If the instance implements the C method then it is itself a class and you can call C on it.
=head3 Relationship to the class function
The L function simply creates an C when it creates a class. It then attaches a C function; this function creates another instance, calls all the C methods from all the C classes on it, then returns it.
=head1 SOURCE REPOSITORY
Lhttp://github.com/jberger/Class-Anonymous
=head1 AUTHOR
Joel Berger, E[email protected]
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2015 by Joel Berger
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.