in reply to Re: Learning classes in Perl
in thread Learning classes in Perl

(This is answer is not intended for the OP, as it will no doubt be over his head...)

To be computer-linguistically pedantic, new is not a constructor, but a factory class method. That is, it's a class method (which in perl means it is invoked with the class name passed as the first argument, rather than an object reference[1]); and it is a factory method, meaning its purpose is to return objects which probably[2] didn't exist prior to the call. POOP does not have constructors. (I can't speak for any other OO frameworks for perl; one or more of them may support something closer to a classical constructor.)

[1] Of course, there are always exceptions. This is Perl, after all. If the sub is written appropriately, it can be both a class method and an object method. Or both of those *plus* support non-method invocation syntax. CGI commits this bletchery, for example.

[2] Of course, there are always exceptions. For example, one common pattern is for the factory method of a class to return the same instance always -- the "singleton".

One consequence of these factoids is that there is nothing inherently special about "new". Using that as the name of the factory method is mere convention. Another consequence is that any method can be a factory method, if it's written to be one. There are no limitations.

I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Replies are listed 'Best First'.
Re^3: Learning classes in Perl
by Anonymous Monk on Feb 20, 2016 at 02:26 UTC

    To be computer-linguistically pedantic, new is not a constructor, but a factory class method.

    Meh, if you use it exactly like a constructor, its a constructor I tells ya

      That's fine; you can say the "new" method of the CGI class is a constructor. But (a) you can't necessarily say that the "new" method of the FuQuux class is a constructor, and (b) you can't necessarily say that the "constructor" of the FuQuux class is named "new". Perl does not require either of these to be the case. It remains true that Perl does not have constructors as the term is understood in other OO languages.

      I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.