http://www.perlmonks.org?node_id=1148459


in reply to "Indirect" object syntax?

I'll offer another linguistic interpretation for you and call this heuristically ambiguous object syntax (in addition to Perldoc's own references to indirect as well.)

The reason for this is spelled out in the perlobj documentation, which recommends the so-called indirect object syntax be avoided.

Consider the code below, which provides both a new and connect constructor.

The instantiation of $obj4 breaks because connect is also a built-in command, and Perl's heuristics prefer it over the connect constructor of the Example class. The same is true for the popular DBI->connect() constructor, as well as many other real-world examples you find in the wild.

It's best not to let something as muddy as heuristics determine what your code does, so I personally avoid such constructs with a passion. Leave that stuff to Java where it belongs ;).

use strict; use warnings; my $obj1 = Example->new(); my $obj2 = new Example; my $obj3 = Example->connect(); my $obj4 = connect Example; package Example; # Constructors # (be subclass friendly, even in example code ;) sub new { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. return bless { }, $class; } sub connect { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. return bless { }, $class; }