Re^2: RFC: Perl meta programming
by bennymack (Pilgrim) on Oct 19, 2006 at 21:31 UTC
|
Thanks for the reply chromatic. I agree that isa() is a better way to validate object references.
I am confused in regards to better ways to validate references however. What's a better way to validate an array ref than "if( 'ARRAY' eq ref $foo )"? Thanks!
| [reply] |
|
if (eval{ use strict 'refs'; @$foo; 1 }) {
# it is, or pretends convincingly, to be an array ref
}
Caveat? Right.
In the presence of ties or overloading, there may be side effects to derefing it. Pick your poison / buyer beware / etc.
print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!
| [reply] [d/l] [select] |
|
The problem I have with isa() is that I either need to use the verbose UNIVERSAL::isa( $foo, 'Object::Foo' ) syntax, or check the return of ref() to be sure that I can do $foo->isa('Object::Foo') without dying.
Wrapping every isa() check in an eval{} seems cumbersome, too.
After a bit of poking, Scalar::Util's blessed() and reftype() look like pretty good options. I suppose I could just import isa(), as well.
| [reply] [d/l] [select] |
|
Beware.
The problem I have with isa() is that I either need to use the verbose UNIVERSAL::isa( $foo, 'Object::Foo' ) syntax...
Unfortunately, it's completely wrong. See Acme::UNIVERSAL::new if you think it's a good idea.
Wrapping every isa() check in an eval{} seems cumbersome, too.
Tough. If you care about getting the right answer, that's what you need to do. Yes, the syntax is a little clunky, but it will give you the right answer. Nothing else will.
I suppose I could just import isa(), as well.
Not if you care about getting the right answer.
| [reply] |
|
|
|
|
|
Re^2: RFC: Perl meta programming
by TGI (Parson) on Oct 19, 2006 at 21:29 UTC
|
Please expand on this observation or point to some resources that can shed some light on your reasoning?
| [reply] |
|
ref gives you a class name. It's easy to fool; bless something into the wrong class name (or worse, a class name such as SCALAR. It's also easy to get wrong -- why do you have to know if something is a hash reference explicitly if the important thing is that you can dereference it as a hash (as in, it's a tied hash or has hash overloading)?
ref ties you to a very specific representation of data, and it's extremely fragile. Fragile code breaks easily. It's a bad thing.
With Scalar::Util's reftype() for the times when I really need to break encapsulation, I can't think of a good reason to keep ref in the language itself.
| [reply] [d/l] [select] |
|
| [reply] |