http://qs1969.pair.com?node_id=573933

jeteve has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks.

Am wondering if it would be possible to add temporary method to a class.

I know how to implement a method dynamically but I wonder how to remove it after it's been used.

So yes, my question is mainly: how to remove a method from a class

Thanks for you enlightments.

-- Nice photos of naked perl sources here !

Replies are listed 'Best First'.
Re: adding temporary method to a class
by ikegami (Patriarch) on Sep 20, 2006 at 16:36 UTC
    { local *Class::method = sub { ... }; ... }

    For anything in "...", called by "..." directly, or called by "..." indirectly, the method will exist.

    I'm not sure doing so is a good idea, however. Why do you want to do this?

      To avoid implementing in the Class a method I only need for a specific algorithm implemented using the visitor pattern.

      Thx a lot.

      -- Nice photos of naked perl sources here !

        This sounds like something for which code references would be more suitable.

        After reading up on the Visitor pattern — I wasn't familiar with it by name — I dare say it *is* something for which code references would be more suitable. The example on that paged could be implemented as follows in Perl:

        use strict; use warnings; { package Wheel; sub new { my ($class, $name) = @_; return bless({ name => $name }, $class); } sub get_name { my ($self) = @_; return $self->{name}; } sub accept { my ($self, $visitor) = @_; $visitor->($self); } } { package Engine; sub new { my ($class) = @_; return bless({}, $class); } sub accept { my ($self, $visitor) = @_; $visitor->($self); } } { package Body; sub new { my ($class) = @_; return bless({}, $class); } sub accept { my ($self, $visitor) = @_; $visitor->($self); } } { package Car; sub new { my ($class) = @_; return bless({ engine => Engine->new(), body => Body->new(), wheels => [ map { Wheel->new($_) } 'front left', 'front right', 'back left', 'back right', ], }, $class); } sub accept { my ($self, $visitor) = @_; $visitor->($self); $self->{engine}->accept($visitor); $self->{body}->accept($visitor); $_->accept($visitor) foreach @{$self->{wheels}}; } } sub visitor { my ($o) = @_; if ($o->isa('Wheel')) { print("Visiting ", $o->get_name(), " wheel\n"); } elsif ($o->isa('Body')) { print("Visiting body\n"); } elsif ($o->isa('Engine')) { print("Visiting engine\n"); } elsif ($o->isa('Car')) { print("Visiting car\n"); } else { die("Unknown class " . ref($o)); } } { my $car = Car->new(); $car->accept(\&visitor); }

        The approach shown above is a nice "lightweight" solution. If you wanted to be more industrial-grade about it, you could make a derived class which adds the new method. Delegation is another possibility.

        We're building the house of the future together.
Re: adding temporary method to a class
by chromatic (Archbishop) on Sep 20, 2006 at 18:23 UTC
Re: adding temporary method to a class
by duckyd (Hermit) on Sep 20, 2006 at 16:57 UTC
    If don't want to add the method just locally, you can remove it using undef *Class::method;. Here's an example:
    my $foo = Foo->new; *Foo::bar = sub { print "bar\n"; }; $foo->bar(); undef *Foo::bar; $foo->bar(); package Foo; sub new { return bless {}, shift; } 1;
    output:
    bar Can't locate object method "bar" via package "Foo" at foo.pl line 9.