On 6/8/07, Robert Hicks <[EMAIL PROTECTED]> wrote:
I see some modules that call "new" like:
my $test = new Some::Module;
and some like:
my $test = Some::Module->new;
Is there a difference and what is the "recommended" way?
Robert
"new Object" uses indirect invocation*. Some people feel it improves
readability; I am not one of those people.
from perldoc perlobj
There is another problem with this syntax: the indirect object is lim‐
ited to a name, a scalar variable, or a block, because it would have to
do too much lookahead otherwise, just like any other postfix derefer‐
ence 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.
To get the correct behavior with indirect object syntax, you would have
to use a block around the indirect object:
move {$obj->{FIELD}};
move {$ary[$i]};
Even then, you still have the same potential problem if there happens
to be a function named "move" in the current package. The "->" nota‐
tion suffers from neither of these disturbing ambiguities, so we recom‐
mend you use it exclusively. However, you may still end up having to
read code using the indirect object notation, so it's important to be
familiar with it.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/