For now, I'll provide two other ways to accomplish what you want, through functional programming or just using Perl itself . . .

Both examples require more functionality in the code higher up. It's not OO due to insufficient encapsulation.

Again, if you don't want to use OO, that's fine, as there are plenty of places where OO is inappropriate. But if you are going to use it, do it right.

This is completely wrong. Now the object designer has to anticipate all of the infinte set of incorrect things that people might try to do with his object and explicitly make each one an error.

No, you don't:

package Employee; . . . sub apply_effect { my ($self, $effects) = @_; # $effects->list returns a list of objects containing # the singular effects foreach my $effect ( $effects->list ) { my $attribute = $effect->attribute; throw_exception() unless exists $self->{$attribute}; # Rest of code } }

Essentially a "deny by default" strategy. Now your test suite just needs to throw a few effects that are known to have attributes it's not going to use, check that exceptions were thrown, and you're set.

It really seems to me that in your game, buildings should just ignore the speed message. But even if your RTS engine is a special case where all sorts of things should ignore all sorts of messages then maybe you're right to do what you're doing but that's still not an argument as to why software in general should do it too.

The important point is that it's going to depend on the application. I was making a general framework, and in certain games it's going to make sense to ignore, while others might want to throw an error. It's none of my businesses to impose that on the game designer. Further, this strategy may be useful outside games, and I'd rather have a general framework that is easily modified rather than writing from scratch.

I can see the argument for using (improved) lvalue subs for internal use (as dragonchild mentioned higher up the thread). But rarely/never outside the class.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.


In reply to Re^15: Assignable Subroutines by hardburn
in thread Assignable Subroutines by dragonchild

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.