in reply to new() is just a constructor by convention right?

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")
  • Comment on (tye)Re: new() is just a constructor by convention right?

Replies are listed 'Best First'.
Re: (tye)Re: new() is just a constructor by convention right?
by merlyn (Sage) on Apr 23, 2001 at 23:55 UTC
    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

(dws)Re: (tye)Re: new() is just a constructor by convention right?
by dws (Chancellor) on Apr 24, 2001 at 00:28 UTC
    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.

(Ovid - constructor names) Re(2): new() is just a constructor by convention right?
by Ovid (Cardinal) on Apr 24, 2001 at 00:05 UTC

    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.