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

NAME

Object::Method - attach method to objects instead of classes.

SYNOPSIS

  package Stuff;
  use Object::Method;

  package main;

  my $o = Stuff->new;
  my $p = Stuff->new;

  # Attach method 'foo' to $o but not $p
  $o->method("foo", sub { ... });

DESCRIPTION

Object::Method lets you attach methods to methods to object but not its classes. There are two different ways to use this module. Keep reading.

The first way is to use it to create a class that allows user to attach methods at runtime. To do this, simply put 'use Object::Method' in your class body like this:

    package Stuff;
    use Object::Method;

This effectively exports a method method to your class, which can be used to create new methods like this:

    my $o = Stuff->new;

    $o->method("foo" => sub { ... });

The method method takes exactly two arguments: the method name, and a sub-routine or code-ref. After calling that method method on object $o, a new method foo will be attached to $o and can be invoked only on $o.

The second way is to use it on all objects through UNIVERSAL::Object::Method like this:

    use UNIVERSAL::Object::Method;
    use SomeClass;

    my $o = SomeClass->new;

    $o->method("foo" => sub { ... });

This is an overwhelming way due to the use of UNIVERSAL namespace. If you are not familiar with it, read the linked documentation.

Please notice that calling the method method multiple times obviously override previous definition and there is no way to undo this for now.

BEHIND THE SCENE

To implement such mechanism, the object on which the method method is invoked are re-blessed into its' own, dynamically created, namespace. You may exam them with ref:

    my $x = My::Awesome::Class->new;
    $x->method("kiss", sub { say ... });

    say ref($x);
    # => My::Awesome::Class#<1>

    say "$x";
    # => My::Awesome::Class#<1>=HASH(0x100826a30)

Those dynamically created classes are properly setup to be inherited from the original classes with @ISA variable. That effects the return value of ref but not isa.

The number in the last part of the namespace is a serial that gets incremented when a new object is encountered. If numerous objects are encountered it might consume a very big part of symble table. The mechanism to reap unused classnames are not implemented at this moment.

AUTHOR

Kang-min Liu <gugod@gugod.org>

SEE ALSO

LICENSE AND COPYRIGHT

Copyright (c) 2010, Kang-min Liu <gugod@gugod.org>.

This is free software, licensed under:

    The MIT (X11) License

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.