The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::Anonymous - Truly private classes with private data for Perl5

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);

DESCRIPTION

Class::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 Package::Anon is available, then no stash entry is created at all. If not, then the classes will actually be blessed into Class::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 Class::Anonymous::Utils which provides "method" in Class::Anonymous::Utils and method modifiers for ease of use.

  my $class = class {
    my ($self) = @_;
    $self->(mymethod = sub { ... });
    my $mymethod = $self->('mymethod');
  }

EXPORTED FUNCTIONS

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 new. Note that subclasses might need more initialization arguments, so you might want to plan for that.

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.

via

  my $subclass = extend $class, via { my ($self) = @_; ... };

Sugar for defining a code reference as a block, simply to make "extend" look better.

OTHER FUNCTIONS

instance

Builds a raw instance of a generic anonymous object. All arguments are the classes to be returned by isa. By default it only fully implements the isa, AUTOLOAD and DESTROY methods. If the instance implements the BUILD method then it is itself a class and you can call new on it.

Relationship to the class function

The "class" function simply creates an instance when it creates a class. It then attaches a BUILD function; this function creates another instance, calls all the BUILD methods from all the isa classes on it, then returns it.

SOURCE REPOSITORY

http://github.com/jberger/Class-Anonymous

AUTHOR

Joel Berger, <joel.a.berger@gmail.com>

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.