in reply to Re: Is "ref($class) || $class" a bad thing?
in thread Is "ref($class) || $class" a bad thing?
But, I must take exception with one of the conclusions you're implying. The error in line 14 is caused by the fact that your new() method isn't verifying that it was actually passed anything.
I know thats why its happening, you know why its happening, but does Joe-Perl-Newbie know why? Maybe, maybe not. The error message given points back to the $foo->test line, which it not the actual source of the issue. IMO, its just adds to the insidiousness of this.
As for a way to solve this, code-wise, I am currently toying with using this:
It works (or rather doesn't work) in all the following conditions:sub new { my ($class) = @_; $class = ref($class) || $class; (UNIVERSAL::isa($class, __PACKAGE__) || UNIVERSAL::isa(__PACKAGE__, $class)) || die "Oh no you don't!!!!"; bless {}, $class; }
As well as works correctly when inherited. I am trying to think of other ways it might get used/abused to see how well it holds up. Any thoughts are appreciated.Foo::new(); Foo::new("SomeOtherModuleName") ; Foo::new([]); Foo::new({});
But, we're just getting stupid. :-)
I know, isn't it fun ;-P
-stvn
UPDATE:
I think this would actually be the most thorough approach, even though I think UNIVERSAL::isa probably handles $class being undef okay.
sub new { my ($class) = @_; $class = ref($class) || $class; (defined $class && (UNIVERSAL::isa($class, __PACKAGE__) || UNIVERSAL::isa(__PACKAGE__, $class))) || die "Oh no you don't!!!!"; bless {}, $class; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Is "ref($class) || $class" a bad thing?
by chromatic (Archbishop) on Jul 12, 2004 at 23:31 UTC | |
by stvn (Monsignor) on Jul 13, 2004 at 01:11 UTC | |
by chromatic (Archbishop) on Jul 13, 2004 at 03:37 UTC | |
by adrianh (Chancellor) on Jul 17, 2004 at 14:31 UTC | |
by merlyn (Sage) on Jul 20, 2004 at 13:32 UTC | |
by adrianh (Chancellor) on Jul 20, 2004 at 16:46 UTC |
In Section
Meditations