in reply to confusion about blessing inside a constructor
They're all wrong. #1 is silly and almost never does the right thing. Ignore it. #2 breaks inheritance. Don't use it. #3 is close, but the presence of the curly braces makes an anonymous hash and it becomes #2 again.
I prefer:
bless $self, $class;You can use the return if you want to be explicit, but if this is the last statement in the constructor, as it often is, there's no need for return.
#1 allows you to call the constructor on an existing object to get a new object, but that's almost never useful unless you do other work to make a copy constructor, and if you do that you ought to document it and by the time you go to that trouble, why not just make a separate method that does something else entirely? (Like I said, silly.)
#2 creates an object in the current package. It ignores the class on which the user called the constructor. Ignoring what the user actually wanted to happen is often a mistake.
#3 just uses the wrong bracketing characters... I hope.
|
|---|