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.

  1. 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.
  2. 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;
  3. 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).

In reply to Re: new() function: incorrect? by JavaFan
in thread new() function: incorrect? by lukeyboy1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.