in reply to Can I add methods to an existing object?
What happened when you tried it?
use strict; package Foo; sub new { my( $c, $v ) = @_; return bless { val => $v }, $c; } sub wubble { print "Wubble: ", shift()->{'val'}, "\n"; }; package main; my $f = Foo->new( "abc" ); $f->wubble; eval { $f->flop; }; if( $@ ) { print "Problem: $@\n"; } eval q{sub Foo::flop { print "Flop: ", scalar reverse( shift()->{'val' +} ), "\n"; }}; eval { $f->flop; }; if( $@ ) { print "Problem: $@\n"; } exit 0; __END__ Wubble: abc Problem: Can't locate object method "flop" via package "Foo" at /tmp/s +quonk line 15. Flop: cba
(Of course if you're serious then Moose and Class::MOP would be less hacky ways to do it.)
The cake is a lie.
The cake is a lie.
The cake is a lie.
|
|---|