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. |