Next Tricks of the Wizards 144

Overloading: Example

        package Vector3;
        use Carp;
        use overload '+' => \&add, 
                     '*' => \&dotproduct,
                     'x' => \&crossproduct,
                    ;
        sub new {
          my $package = shift;
          $package = ref $package || $package;
          croak "Usage: new(x,y,z)" unless @_ == 3;
          my %self;
          @self{'X','Y','Z'} = @_;
          bless \%self => $package;
        }
        ...


Next Copyright © 2003 M. J. Dominus