On Tuesday 26 May 2009 21:22:33 Jonathan Yu wrote: > Chris: > > I'm not sure if that's the most desirable behaviour, as it differs > from the rest of the Perl world... Also, one useful thing is that if > you want to create an object of something in Perl you could do: > > my $foo = Foo::Bar->new(); > my $bar = $foo->new(); > > Which would create a $bar of the same type as $foo. You lose this by > dropping the ->new part.
Actually, Randal Schwartz recommends (and I agree with him) against using - >new() this way to create a new object of the same class as $foo. See: http://www.stonehenge.com/merlyn/UnixReview/col52.html {{{ But here's the problem. When I survey experienced object-oriented programmers, and ask them what they expect new means when called on an instance (without looking at the implementation), the result usually divides rather equally into three camps: those that go ``huh, why would you do that'' and think it should throw an error, those that say that it would clone the object, and those that say it would copy the object's class but not the contents. So, no matter what you intend if you make your new do one of those three things, two thirds of the people who look at it will be wrong. It's not intuitive. So, don't write code like that, and especially don't just cargo- cult that from the manpage into your code. If you want an object like another object, use ref explicitly, as shown above. If you want a clone, put cloning code into your package, and call clone, as we saw earlier. }}} Regards, Shlomi Fish > > I'm sure there's other very good reasons as to why those sorts of > constructors are a bad idea. > > Jonathan > -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ My Aphorisms - http://www.shlomifish.org/humour.html God gave us two eyes and ten fingers so we will type five times as much as we read.
