Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: on the fly methods

by Fletch (Bishop)
on Feb 27, 2007 at 16:35 UTC ( [id://602328]=note: print w/replies, xml ) Need Help??


in reply to on the fly methods

This is Perl, not Ruby. Methods are subs in a given package, not properties of individual instances. You possibly could do some dark magic with AUTOLOAD (make it lookup say $self->{_instancemethods}->{$AUTOLOAD} and call that if it exists), or eval new code into the instance's package (eval qq{ package @{[ ref $self ]}; sub { "NEW CODE" }}; however that's going to make the method available on all instances of that class (and its descendants) not just the one in $self)).

Perhaps you could elaborate on what you're trying to do and you might get better suggestions (e.g. yes, you're trying to emulate Ruby's

class << obj def new_method "fweeee" end end
in order to add a method on a single instance).

Update: Formatting and wording tweaks.

Replies are listed 'Best First'.
Re^2: on the fly methods
by diotalevi (Canon) on Feb 27, 2007 at 16:44 UTC

    If you use any of Class::Trait, Class::MOP, or Moose then objects can have their own methods. (I'm guessing that Moose does this because it uses MOP which provides this service).

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Actually Class::MOP does not provide for a (direct) way to add methods to an instance, it mostly deals with just classes. And with Moose, there is no way to add a single method on a per-instance basis really either, you do it with runtime Roles instead, like this:

      { package My::Class; use Moose; sub hello { "Hello" } package My::Role; use Moose::Role; sub foo { "Foo" } } my $obj_1 = My::Class->new; my $obj_2 = My::Class->new; My::Role->meta->apply($obj_1); print $obj_1->foo; # print "Foo" print $obj_2->foo; # dies with a method not found error
      Also Class::Trait does this same type of thing using Traits instead of Roles.

      Ruby accomplishes this type of thing with "Eigenclasses" which were in the Perl 6 metamodel at one time, but were not backported to Moose (because they tended to make simple metaclass things overly complex). The Ruby idiom of:

      class << obj def new_method "fweeee" end end
      roughly translates into this:
      obj.add_singleton_method(lambda { "fweee" })
      where add_singleton_method is just a method in Object. Something like this could be added to Moose without too much trouble at all really (no AUTOLOAD tricks needed either).

      -stvn
      I had never heard of Moose before this, but I think it's potentially something I could one day use in all my new OO code. Wow, just wow.

      -Paul

        I think Moose is a port of Perl 6's OO system back to Perl 5.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2026-04-10 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.