Hello, monks. I am new to OOP and I'm in the process of creating my very first class. The structure of the objects from this class is a hash of hashes (simplified):

%object okey1 ikey1 = value ikey2 = value okey2 ikey3 = value ikey4 = value

I read the section entitled "Putting All Your Eggs in One Basket" in perltootc from ActiveState, which describes how to use closures to create methods to access class data. I tweeked the code so I could use it to create instance methods:

{ # bare block to limit the scope of %obj_structure # use closures to create accessor methods for each attribute my %obj_structure = ( okey1 => [ qw( ikey1 ikey2 ) ], okey2 => [ qw( ikey3 ikey4 ) ] ); foreach my $okey ( keys %obj_structure ) { foreach my $ikey ( @{ $obj_structure{$okey} } ) { # use symbolic refs to access the symbol table no strict 'refs'; *$var = sub { my $self = shift @_; my ( $value ) = shift @_; if( defined $value ) { $self->{$okey}{$ikey} = $value; } return $self->{$okey}{$ikey}; } } } }

This seems like it would be a great way to easily create instance methods for arbitrarily complex objects, but I wonder whether this approach is an acceptable way to create instance methods. If there is a better/safer way to do this, please enlighten me. For this case I don't need to worry about inheritance, but if there are issues with this approach with respect to inheritance, I'd appreciate knowing where the pitfalls are.

Comments and suggestions are welcome. Thanks in advance!

Update: I just found an eerily similar example of this use in AUTOLOAD - the good, the bad, and the ugly, where it was discussed in the context of AUTOLOAD. A couple of modules were mentioned (Class::MethodMaker and Class::MakeMethods) as an alternative to AUTOLOAD. I'm not sure how to use AUTOLOAD with a complex data structure (methods may have to go through HoA, HoH, HoHoA, etc, depending on $ikey), and I need to keep the number of dependencies (CPAN modules) to a minimum for the users. Is this approach acceptable? Thanks again.

Update: fixed link to perltootc, which apparently has been renamed to perltooc to fit the 8+3 filename scheme (thanks, ihb!)


In reply to Creating instance methods using closures by bobf

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.