Perl tries to detect indirect object syntax using sometimes subtle hints. I was able to break your example easily on the first try:
What did I do? I simply moved package Foo to be after package Bar. That way Perl doesn't know that Foo is a package name when it compiles the line of code containing new Foo(). - tye> perl indobj.pl Bar Undefined subroutine &Bar::Foo called at indobj.pl line 10.
package Bar; sub new { print "Bar\n"; bless {} } sub addFoo { my $self = shift; $self->{Foo} = new Foo(); # which new gets invoked? } package Foo; sub new { print "Foo\n"; bless {} } package main; my $bar = new Bar(); $bar->addFoo(); # if there's a bug, this should tickle it
In reply to Re^2: OO style: Placement of "new" (easy)
by tye
in thread OO style: Placement of "new"
by crenz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |