Hey. I've been experimenting with many new methods of laziness for my roguelike lately, and one of them dealt with the customizability of objects. I've started to incorporate a concept of dynamic attributes, where any attribute of an object can be determined on-the-fly with anonymous subroutines. In other words, $player->{strength} could be a simple value of 14 or a sub returning the current value of $player->{rawstrength} + $weapon->{str_mod} or whatever. Since I use attributes many times in code, I don't want to have to constantly check to see if an attribute is a simple value or an anonymous subroutine. So I wrote a simple interface with tie to make accessing attributes evaluate the anonymous sub automatically and return the value. It works great, but now there's a problem. If I have some other subroutine that needs to take in arguments, I can't use it. $foo->{bar}->($baz) results in Perl trying to dereference whatever $foo->{bar}->() spits out.

To my knowledge, there's no way around this. I don't think there's a way to see how the actual anonymous subroutine is being used. Now that I think about it, situations where dynamic attributes as anonymous subs that take arguments won't really crop up too often. I'm still handling design tentatively. But if they do, is there a solution? Advice is appreciated.

If code is needed, I'd be glad to paste what I have. But all I've got is a tie interface that auto-evaluates anonymous subroutines.

I guess the real solution is to obey proper OOP rules and use real methods. It's just that the syntax is awkward. I'm trying to use the tie interface to create encapsulation that I need, but I'm running into some limitations. I sure hope Perl 6 will remedy these well.


In reply to 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.