Actually, the following are equivalent:
my $object = MyClass->new; my $object = new MyClass;
The second is referred to as the "indirect object syntax". It's generally discouraged because it can confuse the perl parser.
Actually, the first syntax isn't fantastic either, because...
package MyClass { sub new { bless []=>shift } } package OtherClass { sub new { bless []=>shift } } package main { sub MyClass () { return 'OtherClass' } my $object = MyClass->new; print ref $object; }
Now, what does that print? You might expect $object to be an instance of MyClass, but it's actually an instance of OtherClass.
The following are unambiguous ways to call a constructor:
my $object = MyClass::->new; my $object = 'MyClass'->new; my $class = 'MyClass'; my $object = $class->new;
Back to the topic of the indirect object syntax... there's a module called indirect that allows you to convert uses of indirect object syntax into fatal errors.
In reply to Re^2: OOP method usage
by tobyink
in thread OOP method usage
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |