If they wouldn't explain or discuss, I'd say you were well out of it !

I've always worried that the writer of the new() cannot control how it's called, given that any of:

my $foo = Bob::MyObject->new(...) ; # or new Bob::MyObject ... my $foo = $bar->new(...) ; my $foo = Bob::MyObject::new(...) ;
will invoke new(). The last can be distinguished if there are no arguments, and I've seen:
my $class = ref($proto) || $proto || __PACKAGE__ ;
recommended. But it's essentially ambiguous when arguments are present -- I wish for a prototype or other gizmo that would insert an undefined class/object argument when not called as a method.

You can give sub new an empty prototype to ensure that any Bob::MyObject::new calls are either unambigious at run-time or errors at compile-time. (Remembering that method calls ignore prototypes.)

I see the argument that $obj->new(...) is ambiguous, to the extent that, without checking what it does, the programmer doesn't know whether it's: (a) an instance of TMTOWTDI for Class->new(...) (or new Class ...); or (b) an overloading of new meaning clone. Perl Best Practices points out that $foo->new(...) may be a class or an object invocation, so argues that overloading is ambiguous and should be avoided.

Conventions help us reduce complexity, so not overloading new looks reasonable to me. YMMV

Now, if your new is never a clone operation, why would you want to $foo->new(...) -- unless $foo contains a class name (not an object) ? In the interests of, I think, simplicity, the modern advice appears to be to not accept $obj->new(...) -- so the common my $class = ref($proto) || $proto ; spell is deprecated (at least by some).

Of course, if the programmer doesn't provide the right parameters to your object's new you may choose simply to dump them on their backside with a run-time error (deliberately or by omission) -- in some cases, you may have no choice. I could suggest that in the case of $obj->new(...) you have a choice. But, if you go around pandering to slack programmers who ought to know better, how are they going to learn ? Really.

[In the context of cargo-cults: I guess you could say that not allowing for $obj->new(...) looks sufficient to eliminate slack practice -- whether it's necessary I leave open.]


In reply to Re: new() function: incorrect? by gone2015
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.