Vector: Difference between revisions
m
→{{header|Perl}}: do the same job, but less verbosely
Hedgehog7777 (talk | contribs) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: do the same job, but less verbosely) |
||
Line 2,090:
=={{header|Perl}}==
Typically we would use a module, such as [https://metacpan.org/pod/Math::Vector::Real Math::Vector::Real] or [https://metacpan.org/pod/Math::Complex Math::Complex]. Here is a very basic Moose class.
<syntaxhighlight lang="perl">
use Moose;▼
package Vector;
▲use Moose;
'-' => \&sub,▼
use
'
'/' => \&div,
'""' => \&stringify;
Line 2,103:
has 'y' => (is =>'rw', isa => 'Num', required => 1);
sub mul ($a, $b, $) { Vector->new( x => $a->x
▲ Vector->new( x => $a->x - $b->x, y => $a->y - $b->y);
▲ Vector->new( x => $a->x * $b, y => $a->y * $b);
▲ Vector->new( x => $a->x / $b, y => $a->y / $b);
▲ "(" . $self->x . "," . $self->y . ')';
package main;
|