holyghost has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I would like to know how I override an operator such as * for e.g. this 3d engine pseudo code :
sub matrix_calculate { my ($self, $matrix) = shift; ###return my ($matrix4) = $self->{$matrixX} * $self->{$matrixY} * $self->{$mat +rixZ} * $self->{$matrix}; }
The meaning of the XYZ matrices are the rotation matrices with sin and cos.

Replies are listed 'Best First'.
Re: perl vs. C++ operators
by Your Mother (Archbishop) on Oct 09, 2017 at 14:58 UTC

    You want overload; its doc is quite good so I am not going to attempt a rehashed synopsis.

    I recommend you take a good look at PDL before you go ahead with what you're doing though.

Re: perl vs. C++ operators
by Corion (Patriarch) on Oct 09, 2017 at 14:58 UTC

    See the overload pragma, which allows you that.

    Also, note that assignment is not really overloadable and some other caveats to overload.

Re: perl vs. C++ operators
by LanX (Saint) on Oct 09, 2017 at 14:58 UTC
    $self->{$matrixX} and the others need to be objects of a class (basically blessed to Package ) which overloads the operators.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: perl vs. C++ operators
by Anonymous Monk on Oct 09, 2017 at 16:18 UTC
    You don't necessarily have to roll your own matrix class, there are a number of them out there already. If you're doing a lot of numerical work, you might like PDL::Matrix (though it uses x for matrix multiplication, leaving * for elementwise multiplication). If you're doing exclusively 3d work, you might want something specialized for that purpose, but I don't have a module to suggest.
Re: perl vs. C++ operators
by etj (Priest) on May 08, 2022 at 18:01 UTC
    For 3D work in PDL, there's PDL::Graphics::TriD to visualise things in various ways.

    There's also PDL::Graphics::TriD::Quaternion, not hyperlinked because not indexed by MetaCPAN; it's in a slightly rough state (e.g. doesn't have the obvious overloads), but is used by the UI interaction code, so actually works very well.