SPUG: method call during object construction

DeRykus, Charles E charles.e.derykus at boeing.com
Wed Feb 15 15:43:50 PST 2006


> 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