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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.