What does the following code do?
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!my $effect = Effect->new; $effect->set( salary => 1.02, '*' ); # A 2% raise $employee->apply_effect( $effect );
The exact same effect can be achieved by having all your objects inherit from a base class with
then go back to usingsub AUTOLOAD { # do nothing }
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.$employee->salary( $employee->salary * 1.02 );
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |