Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

adding singleton methods to objects

by andal (Hermit)
on Jun 04, 2009 at 13:28 UTC ( #768428=snippet: print w/replies, xml ) Need Help??
Description: 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
my $obj = DynObject->new(id => 'MyObj', action => sub{print "hi\n";}); print $obj->id, "\n"; $obj->action();
or as base class
$obj = MyDyn->new(id => 'MyObj'); print $obj->id, "\n"; $obj->action(); package MyDyn; use base 'DynObject'; sub action { print "hi\n"; }
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;
        $type = __PACKAGE__;
        $type .= "::obj@{[$counter++]}";
        *{"${type}::ISA"} = [$class];
    for(my $i = 0; $i < @_; $i+=2)
        croak("The method name '$_[$i]' is not a word")
            unless $_[$i] =~ /^\w+$/ && $_[$i] !~ /^\d+$/;
        if(ref $_[$i+1] eq 'CODE')
            *{"${type}::$_[$i]"} = $_[$i+1];
        elsif(defined $_[$i+1] && !ref $_[$i+1])
            my $str = $_[$i+1];
            *{"${type}::$_[$i]"} = sub{$str};
            delete ${"${type}::"}{$_[$i]};
    return ref $class ? $class : bless [], $type;

    my $obj = shift;
    my $type = ref $obj;
    $type =~ s/(\w+)$//;
    my $name = $1 . "::";
    no strict 'refs';
    delete ${$type}{$name};

Replies are listed 'Best First'.
Re: adding singleton methods to objects
by merlyn (Sage) on Jun 04, 2009 at 15:17 UTC
    Or, just look at Class::Prototyped, a full-featured, mature (used in production) solution of this. There are likely also some MooseX:: modules that implement prototype-based behavior as well.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://768428]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-02-07 18:03 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (40 votes). Check out past polls.