Seems like it'd be less work to write:sub new { croak "new() called as a function, not a method\n" unless @_; bless {}, shift; }
As dragonchild already pointed out, this would not handle Foo::new({}), nor would it handle Foo::new("Data::Dumper"). Both are things are equally as stupid for a user to do, but a new user may not realize the method/function difference, the real question is whether that is my reponsibility or not.
If you really want to be paranoid, though, you should make something similar a precondition of all of your methods.
I assume you mean to check $self, that is true, but even I think that would a little much.
Also, you ought to call isa() directly, not through UNIVERSAL::, in case someone overrides it.
That too would not work for Foo::new({}) or other such insanity.
I am really just trying to find something in between totally over-the-top paranoid, and you-break-it-you-buy-it-an-I-dont-care. And I am still looking as I think the example I gave above it probably too much.
In reply to Re^4: Is "ref($class) || $class" a bad thing?
by stvn
in thread Is "ref($class) || $class" a bad thing?
by stvn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |