Overriding object methods (sometimes)

Yuval Kogman nothingmuch at woobling.org
Mon Feb 25 18:06:10 GMT 2008


What about delegation?

	sub foo {
		$self->real_ua->foo;
	}

BTW, you can use Class::MOP or some other introspection capable
module to generate the wrappers:
	
	my $wrapper_meta = Class::MOP::Class->initialize(__PACKAGE__);

	foreach my $method_name ( Class::MOP::Class->initialize("LWP::UserAgent")->compute_all_applicable_methods ) {
		$wrapper_meta->add_method( $method_name, sub {
			my ( $self, @args ) = @_;

			$self->wrapper( $method_name, @args );
		});
	}

	sub wrapper {
		my ( $self, $method, @args ) = @_;

		# ... and eventually:
		$self->real_ua->$method(@args);
	}

Cheers,

-- 
  Yuval Kogman <nothingmuch at woobling.org>
http://nothingmuch.woobling.org  0xEBD27418



More information about the london.pm mailing list