Note: I typed this code straight into the textbox. I have no clue if it will even compile, let alone work as intended.
package Dabreegster::Attribute::Maker; use strict; use warnings; # if $] >= 5.6.0; sub import { shift; my $pkg = caller; foreach my $attr ( @_ ) { # We're doing symbol-table manipulation here, so # the prohibition against soft references needs to # be relaxed, but only within this context. no strict 'refs'; my $getter = "${pkg}::${attr}"; my $setter = "${pkg}::set_${attr}"; # We will only define the function if the function # hasn't already been defined. *{ $getter } = sub { my $self = shift; if ( (Scalar::Util::reftype( $self->{$attr} ) || '') eq 'C +ODE' ) { return $self->{$attr}->( @_ ); } else { return $self->{$attr}; } } unless defined \&{ $getter }; *{ $setter } = sub { my $self = shift; if ( @_ ) { $self->{$attr} = shift; } return $self; } unless defined \&{ $setter }; } return; } 1; __END__
Then, you would use it as so:
package Character::Player; use Dabreegster::Attribute::Maker qw( str dex con int wis cha ); # Now, you have access to the following functions: # str(), set_str(), dex(), set_dex(), etc.
This code plays nicely with inheritance so long as all your object representations are hashrefs. But, given your original design, I don't think that's going to be a problem.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Re: Using tie to return value of anonymous sub by dragonchild
in thread Using tie to return value of anonymous sub by dabreegster

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.