On 3/11/09 Wed  Mar 11, 2009  5:28 AM, "Dermot" <paik...@googlemail.com>
scribbled:

> 2009/3/11 Dr.Ruud <rvtol+use...@isolution.nl>:
>> Dermot wrote:
>> 
>>> I created  a small Class, initially with Moose. When I wanted an
>>> instance of the class I would call `my $instance = new MyClass`;
>>> 
>>> I then removed Moose  and went for a standard Perl 00 constructor:
>>> 
>>> sub new {
>>>  my $class = shift;
>>>  my $self = {};
>>>  $self->{config} = _get_config();
>>>  bless ($self, $class);
>>>  return $self;
>>> }
>>> 
>>> I haven't changed the way I created an instance, and it still works. I
>>> would have thought I would have to create instances now with
>>> 
>>> my $instance = MyClass->new;
>>> 
>>> Are both assignments legit?
>> 
>> There is even a third way:
>> 
>>    MyClass::->new
>> 
>> and a fourth way:
>> 
>>    MyClass::->new()
> 
> So do they all amount to the same thing? I can see that the 4th way
> would allow you to pass arguments. When you read the docs for modules
> they (all the one's I can recall) show either
> 
> my $inst = new Some::Module;
> 
> or
> 
> my $inst = Some::Module->new;
> 
> I thought there was some black art to allow you to make a constructor
> work with the first example.

They are mostly equivalent (you can add argumets to the first call, too).
There are potential compiler problems with the first form, as it is more
difficult for the compiler to figure out which package's new method should
be called if you generate the module name with a function or constant. For
this reason, the form Some::Module->new() is preferred.

See "Object Oriented Perl", by Damian Conway, pp 98 for details.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to