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

Hi all,

I have a question about the arrow operator, I understand the definition of ->, but I didn't know how to interpret "return $a->X*$b->X+$a->Y*$b->Y+$a->Z*$b->Z" in following code.

sub InProduct(){ my($a, $b) = @_; return $a->X*$b->X+$a->Y*$b->Y+$a->Z*$b->Z}

Thanks!

Have a good day.

Winston

2017-08-29 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: about the application of arrow operator
by roboticus (Chancellor) on Aug 29, 2017 at 00:04 UTC

    windcrazy86:

    It looks like $a and $b are objects of a class having member functions X, Y and Z. Also, based on the name of the subroutine, it's simply computing the inner product of vectors $a and $b. So imagine that $a->X returns the X component of vector $a, $a->Y the Y component, etc.

    So the function is computing (A.x*B.x) + (A.y*B.y) + (A.z*B.z).

    $ cat foo.pl use strict; use warnings; sub new_vec { return bless { vec=>[ @_ ] }, 'flarb' }; my $a = new_vec(1, 2, 3); my $b = new_vec(1, 4, 9); my $t = InProduct($a, $b); print "t=$t\n"; sub InProduct { my ($a, $b) = @_; return $a->X*$b->X + $a->Y*$b->Y + $a->Z*$b->Z; } package flarb; sub X { my $self=shift; return $self->{vec}[0] }; sub Y { my $self=shift; return $self->{vec}[1] }; sub Z { my $self=shift; return $self->{vec}[2] }; $ perl foo.pl t=36

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Cool! Thank you very much! Now it's clear for me.
Re: about the application of arrow operator
by kcott (Archbishop) on Aug 29, 2017 at 04:38 UTC

    G'day Winston,

    Welcome to the Monastery.

    From your question, I'm guessing this isn't your code; regardless, adding whitespace will usually make the code clearer. Here's how Perl sees it:

    $ perl -MO=Deparse -e '$a->X*$b->X+$a->Y*$b->Y+$a->Z*$b->Z' $a->X * $b->X + $a->Y * $b->Y + $a->Z * $b->Z; -e syntax OK

    If you're uncertain about the precedence, use the '-p' switch:

    $ perl -MO=Deparse,-p -e '$a->X*$b->X+$a->Y*$b->Y+$a->Z*$b->Z' ((($a->X * $b->X) + ($a->Y * $b->Y)) + ($a->Z * $b->Z)); -e syntax OK

    See also: B::Deparse; "perlop: Operator Precedence and Associativity"; and, perlop in general for operator documentation (for instance, "The Arrow Operator" section has links to information that you may not be aware of, such as the fairly new postfix dereferencing).

    — Ken

Re: about the application of arrow operator
by LanX (Saint) on Aug 29, 2017 at 00:07 UTC
    Please use code tags!

    The code you are showing is calculating the sum of products of the x y and z component of two vectors a and b.

    The arrows have the highest precedence in this calculation, then the multiplication and last the addition.

    See "algebraic definition" in http://en.wikipedia.org/wiki/Dot_product for mathematical background.

    a1 is $a->X and so on. ..

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie! </""sub>

Re: about the application of arrow operator
by AnomalousMonk (Archbishop) on Aug 29, 2017 at 03:41 UTC
    sub InProduct(){ my($a, $b) = @_; return $a->X*$b->X+$a->Y*$b->Y+$a->Z*$b->Z}

    A side note: The  InProduct() function is defined with a prototype that specifies an empty argument list, but the function only makes sense if it's called with two arguments. You are obviously calling the function in a way that defeats prototype checking; don't bother with prototypes in such cases — or in most other cases. See also Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.


    Give a man a fish:  <%-{-{-{-<

Re: about the application of arrow operator
by 1nickt (Canon) on Aug 29, 2017 at 00:05 UTC

    Hi, Did you try passing a couple of numerical values to the sub to see what it produces?

    Based on what you wrote those are object methods and the return value of the sub is the product of a simple mathematical operation, wherein standard operator precedence rules would apply, i.e. multiplication before addition.

    By the way, please edit your post to enclose your code in the appropriate tags, as shown below the preview box.


    The way forward always starts with a minimal test.
Re: about the application of arrow operator
by stevieb (Canon) on Aug 29, 2017 at 00:50 UTC

    Don't use $a and $b as variables. They have special use in certain situations.

Re: about the application of arrow operator
by Anonymous Monk on Aug 29, 2017 at 12:33 UTC
    Assuming that you have an extraneous '}' in that line, spacing and parentheses will do wonders for readability:
     return ($a->X * $b->X) + ($a->Y * $b->Y) + ($a->Z * $b->Z);