in reply to Re: (cLive ;-) Re: Perl High School
in thread Perl High School

dave - can you explain in a little more detail why not? No-ones pointed this out to me before...

thanks

cLive ;-)

--
seek(JOB,$$LA,0);

Replies are listed 'Best First'.
Re: (cLive ;-) Re: Perl High School
by davorg (Chancellor) on Feb 22, 2002 at 07:28 UTC

    For more details see this node (and for even more details, the pages quoted in that node from The Perl Cookbook and Object Oriented Perl).

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: (cLive ;-) Re: Perl High School
by quikwit (Beadle) on Feb 22, 2002 at 14:12 UTC
    Here's the (HTML-ized) warning from perlop. Note the word "exclusively" at the end:

    WARNING

    While indirect object syntax may well be appealing to English speakers and to C++ programmers, be not seduced! It suffers from two grave problems.

    The first problem is that an indirect object is limited to a name, a scalar variable, or a block, because it would have to do too much lookahead otherwise, just like any other postfix dereference in the language. (These are the same quirky rules as are used for the filehandle slot in functions like "print" and "printf".) This can lead to horribly confusing precedence problems, as in these next two lines:

    move $obj->{FIELD}; # probably wrong! move $ary[$i]; # probably wrong!

    Those actually parse as the very surprising:

    $obj->move->{FIELD}; # Well, lookee here $ary->move->[$i]; # Didn't expect this one, eh?

    Rather than what you might have expected:

    $obj->{FIELD}->move(); # You should be so lucky. $ary[$i]->move; # Yeah, sure.

    The left side of "->" is not so limited, because it's an infix operator, not a postfix operator.

    As if that weren't bad enough, think about this: Perl must guess (at compile time) whether name and move above are functions or methods. Usually Perl gets it right, but when it doesn't it, you get a function call compiled as a method, or vice versa. This can introduce subtle bugs that are hard to unravel. For example, calling a method "new" in indirect notation--as C++ programmers are so wont to do--can be miscompiled into a subroutine call if there's already a "new" function in scope. You'd end up calling the current package's "new" as a subroutine, rather than the desired class's method. The compiler tries to cheat by remembering bareword "require"s, but the grief if it messes up just isn't worth the years of debugging it would likely take you to to track such subtle bugs down.

    The infix arrow notation using "->" doesn't suffer from either of these disturbing ambiguities, so we recommend you use it exclusively.