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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Learning classes in Perl
by Anonymous Monk on Feb 20, 2016 at 02:26 UTC | |
by jdporter (Paladin) on Feb 20, 2016 at 16:48 UTC |