What does the following code do?

my $effect = Effect->new; $effect->set( salary => 1.02, '*' ); # A 2% raise $employee->apply_effect( $effect );
All one can say it that it might raise the salary by 2% or it might do nothing at all, not even report an error. So now we have a Perl that doesn't give you compile time checking and doesn't give you run time checking either!

The exact same effect can be achieved by having all your objects inherit from a base class with

sub AUTOLOAD { # do nothing }
then go back to using
$employee->salary( $employee->salary * 1.02 );
Yes this would be an incredibly bad idea but it's exactly equivalent to using effect objects and it's a hell of a lot easier.

There are some times when you want missing fields to be ignored but you should make that explicit in your code by doing something like

$employee->salary( $employee->salary * 1.02 ); # we don't mind if they don't have a frobnitz eval { $employee->frobnitz( $employee->frobnitz + 1.5 ) };

Problems with effect objects

The only real use I can think of for an effect object is to use it as a callback mechanism. Various people can add effects (or even remove or reorder effects) as the object is passed around, but I still don't see a reason to make it ignore errors by default.


In reply to Re^12: Assignable Subroutines by fergal
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.