<?xml version="1.0" encoding="windows-1252"?>
<node id="768428" title="adding singleton methods to objects" created="2009-06-04 09:28:07" updated="2009-06-04 09:28:07">
<type id="1980">
snippet</type>
<author id="767495">
andal</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
This piece of code may serve as the base class for objects that need singleton methods (methods specific for this object and not for whole class). One can use it directly

&lt;CODE&gt;
my $obj = DynObject-&gt;new(id =&gt; 'MyObj', action =&gt; sub{print "hi\n";});

print $obj-&gt;id, "\n";
$obj-&gt;action();
&lt;/CODE&gt;

or as base class

&lt;CODE&gt;

$obj = MyDyn-&gt;new(id =&gt; 'MyObj');
print $obj-&gt;id, "\n";
$obj-&gt;action();

package MyDyn;
use base 'DynObject';
sub action
{
   print "hi\n";
}
&lt;/CODE&gt;</field>
<field name="snippetcode">
&lt;CODE&gt;
use strict;
package DynObject;
use Carp;

my $counter = 0;

sub new
{
    my $class = shift;
    croak("The number of parameters must be even") unless @_ % 2 == 0;

    no strict 'refs';
    my $type = ref $class;
    my $code;
    if(!$type)
    {
        $type = __PACKAGE__;
        $type .= "::obj@{[$counter++]}";
        *{"${type}::ISA"} = [$class];
    }
    for(my $i = 0; $i &lt; @_; $i+=2)
    {
        croak("The method name '$_[$i]' is not a word")
            unless $_[$i] =~ /^\w+$/ &amp;&amp; $_[$i] !~ /^\d+$/;
        if(ref $_[$i+1] eq 'CODE')
        {
            *{"${type}::$_[$i]"} = $_[$i+1];
        }
        elsif(defined $_[$i+1] &amp;&amp; !ref $_[$i+1])
        {
            my $str = $_[$i+1];
            *{"${type}::$_[$i]"} = sub{$str};
        }
        else
        {
            delete ${"${type}::"}{$_[$i]};
        }
    }
    return ref $class ? $class : bless [], $type;
}

sub DESTROY
{
    my $obj = shift;
    my $type = ref $obj;
    $type =~ s/(\w+)$//;
    my $name = $1 . "::";
    no strict 'refs';
    delete ${$type}{$name};
}

1;

&lt;/CODE&gt;</field>
</data>
</node>
