Adding Perl Methods during Runtime

Since perl objects are blessed references, it is not easy to add an instance method which is visible in one single instance.

NOTE: Please read and understand the note about adding functions.

But you can work around that:

#!/usr/bin/perl

use strict;
use warnings;

### creates two instances of Test
my $object1 = Test->new();
my $object2 = Test->new();

### adds a dynamic method 'test' to object1
$object1->create('test', sub {
    my ($self, $param) = @_;
    print 'hallo' . $param . "n";
});

### calls methods
print 'start' . "n";
$object1->test(1);
$object1->test(2);
$object2->test(3);
print 'stop' . "n";

package Test;

use Carp;

### simple constructor
sub new {
    my ($pkg) = @_;

    return bless {
        'methods' => {},
    }, $pkg;
}

### creates dynamic methods
sub create {
    my ($self, $name, $code) = @_;

    ### saves code in the blessed reference
    $self->{'methods'}->{$name} = $code;

    ### strict refs break *$name
    no strict 'refs';

    ### globally visible function
    *$name = sub {
        my $self = shift;

        ### dereferences code and executes it
        if (exists($self->{'methods'}->{$name})) {
            &{$self->{'methods'}->{$name}}($self, @_);

        ### dies if the curent instance does not define code for $name
        } else {
            confess $self . ' does not implement ' . $name . "n";
        }
    };
}

1;

NOTE: I have implemented this in the current development version of my perl code Library.

See also: Dynamic code considerations