SPUG: method call during object construction

Umar Cheema umar at drizzle.com
Wed Feb 15 15:57:21 PST 2006


> This'll work though:
>
> (my $obj = Class->new('class_name'))->change_name('new_class_name');

Ah, that's exactly what I wanted to try and see if that worked in Perl. I 
remember using something similar in Java where you could make a method 
call by putting paranthesis around the newly created object and 
invoking methods on it.

Thank you, that helps.
Umar

On Wed, 15 Feb 2006, DeRykus, Charles E wrote:

 

> I was hoping to make this work:

> my $obj = Class->new('class_name')->change_name('new_class_name');
> $obj->do_other_things();
> ....

> But after the constructor call $obj simply holds the value returned by
the 'change_name' method.

> Of course the following works just fine:

> my $obj = Class->new('class_name');
> $obj->change_name('new_name');
> $obj->do_other_things();
> ...

> Can someone explain what is going on with the first way of calling a
method during construction? Is > this something we're not suppose to do
or am I just seeing weird perl behavior for some other 
> reason?

This'll work though:

(my $obj = Class->new('class_name'))->change_name('new_class_name');

-- 
Charles DeRykus







More information about the spug-list mailing list