princepawn has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking at a Perl test that somebody crafted up, and they have a multiple choice question:
Which of the following is a constructor in Perl: a. bless b. return c. :: d. new -- answer

But in my eyes new has NO special status to Perl under ANY circumstances and it is MERELY by convention that one returns newly blessed references to something or the other in a subroutine named new()

Am I rite or rong?

Replies are listed 'Best First'.
(tye)Re: new() is just a constructor by convention right?
by tye (Sage) on Apr 23, 2001 at 23:49 UTC

    Yes, the question should be reworded to "Which of the following is an object constructor in Perl?" and the answer is "depends on the module". Though "bless" is almost a better answer than "new" as are "TIESCALAR", etc.

    Though the tradition of using "new" is so strong that I can understand that answer. I can't, at the moment, think of a single object module that doesn't provide "new" as a constructor.

            - tye (but my friends call me "Tye")
      Though the tradition of using "new" is so strong that I can understand that answer. I can't, at the moment, think of a single object module that doesn't provide "new" as a constructor.
      DBI, for one. Uses connect.

      -- Randal L. Schwartz, Perl hacker

      There's a difference between a "constructor" method, which gives you back a newly created (and possibly initialized) instance of a named class, and a "factory" method, which you invoke on an instance of one class to get back a newly created (and possibly initialized) instance of a different class.

      The difference is in whether or not you need to know the class name. Factory methods "hide" that need, which is a Very Good Thing when you're building class hierarchies that other might extend.

      DBI's prepare is a factory method, not a constructor.

      Another example of constructors not names "new" would be in the TK module. All constructors in this module are named after the widgets that they create.

      DBI also uses a "prepare" constructor.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: new() is just a constructor by convention right?
by dws (Chancellor) on Apr 23, 2001 at 23:37 UTC
    Discussed at some length in Curious Constructor Syntax.

    The short answer is that new returns a newly blessed object by convention. You could as easily write something that doesn't always return a new object. Consider:

    package Singleton; ... my $singleton = undef; sub new { my $pkg = shift; $singleton ||= bless {}, $pkg; } ... my $frob = new Singleton();
Re: new() is just a constructor by convention right?
by Albannach (Monsignor) on Apr 23, 2001 at 23:43 UTC
    Though I'm a newbie with respect to PerlOO, it looks to me like perlman:perlobj pretty much says this, and in fact suggests that new() isn't a good choice for a name as it may lead to confusion among those used to C++:
    That word new isn't special. You could have written a construct this way, too...

    --
    I'd like to be able to assign to an luser