Check to see if you're being asked to do something that you know how to do, if so, handle it. If not, let the object you wrapped handle it. I made a small modification: the $self hash reference didn't need to be a reference so I promoted it to a regular hash. Your closure closes over that just fine.
That is, the basic technique is wrapping and delegation. That's it. There's no special magic involved because it's a closure or because %self is a hash or a hash reference. That's probably why you didn't find anything specifically about this, it's a general technique and not something that requires anything new of you.
package YourExtension; use base 'TheOriginal'; sub new { my $class = shift; my $extended = $class->SUPER::new( @_ ); my %self = ( CELLULAR => undef, EMAIL => undef, ); my $closure = sub { my $field = shift; if ( exists $self{$field} ) { if ( @_ ) { $self{$field} = shift } return $self{$field}; } else { return $extended->$field( @_ ); } }; bless( $closure, $class ); return $closure; }
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
In reply to Re: OO: extending a closure object
by diotalevi
in thread OO: extending a closure object
by gargle
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |