in reply to Re^2: Can I add methods to an existing object?
in thread Can I add methods to an existing object?
First, I thought of something like this:
*Log::Dispatch::warn = *Log::Dispatch::warning;But I thought, that it doesn't look nice ;) so I changed it to:
*Log::Dispatch::warn = \&Log::Dispatch::warning;But both threw warnings that Log::Dispatch::warn is "used only once: possible typo..."; And then, based upon the answers from morgon and mr_mischief I thought of
sub Log::Dispatch::warn { my $self = shift; $self->warning(@_); }
tested with:
#!/usr/bin/perl -l use strict; use warnings; package foo; sub new { bless {}, shift(); } sub foo { my $self = shift; print "hello @_"; } package main; sub foo::bar { my ( $self ) = shift; $self->foo(@_); } *foo::baz = \&foo::foo; *foo::buzz = *foo::foo; my $o = foo->new(); $o->bar('world'); $o->baz('world'); $o->buzz('world'); __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Can I add methods to an existing object?
by educated_foo (Vicar) on Apr 03, 2009 at 13:18 UTC | |
by linuxer (Curate) on Apr 03, 2009 at 15:28 UTC | |
by educated_foo (Vicar) on Apr 03, 2009 at 17:39 UTC | |
by Jenda (Abbot) on Apr 04, 2009 at 13:46 UTC | |
by linuxer (Curate) on Apr 03, 2009 at 17:47 UTC | |
by Your Mother (Archbishop) on Apr 03, 2009 at 17:58 UTC |