Whether it's wrong or not depends on your viewpoint. I can identify at least three issues in your
new methods I've seen people argueing against.
- People object to give the programmer liberty to create a new object of the same class as a given $obj by doing $new_obj = $obj->new, insisting the programmer must jump hoops and write $new_obj = (ref $obj)->new, because they themselves get confused, insisting that if an object method is called new it must return a clone.
- People object to integrating object construction with object initialization, arguing they are two different operations and should be into different function. There argument is that combining object construction with object integration makes it harder to do multiple inheritance. They argue it's better written as:
package Bob;
sub new {bless({}, shift)->init}
sub init {
my $self = shift;
$self->{command} = undef;
$self;
}
my $bob = Bob->new;
That way, you can do MI more easily:
package MyUncle;
sub new {bless({}, shift)->init}
sub init {
my $self = shift;
$self->{topping} = "dessert";
$self;
}
package BobIsMyUncle;
our @ISA = ('Bob', 'MyUncle');
sub new {bless({}, shift)->init};
sub init {
my $self = shift;
$self->Bob::init->MyUncle::init;
$self->{floor}='wax';
$self
}
my $bob_is_my_uncle = BobIsMyUncle->new;
- Some people say that using hashrefs as objects is wrong, as it leads to the possibility of name clashes of object attributes when inheriting (a subclass of Bob::MyObject may use 'command' itself as well). And that if you typo a hashkey name, you will not get a compile time error, just odd runtime behaviour. They prefer you use Class::InsideOut or some other module implementing classes for you.
I typically ask how people prefer to implement their objects as well during an interview. Unless they really screw up, there aren't wrong answers. What interests me is the motivation (I ask for that). I don't care whether they have the same preference as I have, as long as they can motivate them (and bonus points for identifying the weaknesses in their choices).