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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |