in reply to Re^3: Is "ref($class) || $class" a bad thing?
in thread Is "ref($class) || $class" a bad thing?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Is "ref($class) || $class" a bad thing?
by chromatic (Archbishop) on Jul 13, 2004 at 03:37 UTC |