Next Tricks of the Wizards 145

Overloading: Example

        sub add {
          my ($vec1, $vec2) = @_;
          unless (ref $vec1 && $vec1->isa('Vector3') 
               && ref $vec2 && $vec2->isa('Vector3')) {
            croak "Invalid vector addition";
          }
          $vec1->new(map {$vec1->{$_} + $vec2->{$_}}  qw(X Y Z));
        }


Next Copyright © 2003 M. J. Dominus